locked
Entity Framework: Read table mapping OnModelCreating RRS feed

  • Question

  • Hello,

    I am using Code First Approach for creating the database tables using entity framework 6.0.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
                modelBuilder.Entity<ErrorLogModel>().ToTable("ApplicationErrorLog", "dbo");

    }

    However, I would like to read this mapping in some other program, to identify the difference between properties in class/model vs. database table columns.

    So that in future if there is any change in class/model, we could replicate the same changes in database automatically.

    • Moved by CoolDadTx Wednesday, July 18, 2018 2:46 PM EF related
    Wednesday, July 18, 2018 2:31 PM

Answers

  • Hi AjayKumar_MIND,

    If you want to get table name at run-time, please refer to the following extension. 

    public static class ContextExtensions
        {
            public static string GetTableName<T>(this DbContext context) where T : class
            {
                ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
    
                return objectContext.GetTableName<T>();
            }
    
            public static string GetTableName<T>(this ObjectContext context) where T : class
            {
                string sql = context.CreateObjectSet<T>().ToTraceString();
                Regex regex = new Regex(@"FROM\s+(?<table>.+)\s+AS");
                Match match = regex.Match(sql);
    
                string table = match.Groups["table"].Value;
                return table;
            }
        }

    #Usage

    using (var db = new BloggingEntities())
    {
         var tableName = db.GetTableName<ErrorLogModel>();
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 19, 2018 2:17 AM

All replies

  • I would recommend that you move your data layer to a separate assembly that can be loaded in multiple programs. Then you can query for the information. To make this easier I would recommend that you use data annotations for the scenario of a table/schema mapping.

    [Table("ApplicationErrorLog")]
    public class ErrorLogModel
    {
       ...
    }

    You can apply annotations for pretty much everything. You can then remove the explicit mapping you're using. I've found that the relationships are best defined using type configurations instead. If you are heavily using type configurations then you'll probably end up having to create the model builder, run the creation logic and then compare the model build configuration after the fact.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 18, 2018 2:46 PM
  • Hi AjayKumar_MIND,

    If you want to get table name at run-time, please refer to the following extension. 

    public static class ContextExtensions
        {
            public static string GetTableName<T>(this DbContext context) where T : class
            {
                ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
    
                return objectContext.GetTableName<T>();
            }
    
            public static string GetTableName<T>(this ObjectContext context) where T : class
            {
                string sql = context.CreateObjectSet<T>().ToTraceString();
                Regex regex = new Regex(@"FROM\s+(?<table>.+)\s+AS");
                Match match = regex.Match(sql);
    
                string table = match.Groups["table"].Value;
                return table;
            }
        }

    #Usage

    using (var db = new BloggingEntities())
    {
         var tableName = db.GetTableName<ErrorLogModel>();
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 19, 2018 2:17 AM