如何获取对所有Entity Framework EntityTypeConfiguration的引用

时间:2018-02-14 23:36:41

标签: asp.net asp.net-mvc entity-framework

我正在使用Entity Framework 6和ASP.NET,我遇到了这个问题:

假设我在2个模型A,B上配置了这2个实体映射:     class A {string property1 {get; set},string property2 {get;设置}}     class B {string property1 {get; set},string property2 {get;设置}}

public class AConfiguration : EntityTypeConfiguration<A> {
    ToTable("TableA");
    Property( x => x.propperty1 ).maxLength(10).HasColumnName("Column1");
    Property( x => x.property2 ).maxLength(10).HasColumnName("Column2");
}

public class BConfiguration : EntityTypeConfiguration<B> {
    ToTable("TableB");
    Property( x => x.propperty1 ).maxLength(10).HasColumnName("Column1");
    Property( x => x.property2 ).maxLength(10).HasColumnName("Column2");
}

假设我已经完成了为我的项目配置EF并且读取和写入数据库工作完美。

现在我的问题是:有没有办法以字典格式检索我们在上面配置的所有属性? 例如:

//This would return a dictionary that looks somehow like this:
/* {
     "TableA": { 
                   property1: { 
                                  maxLength: 10
                              }
                   property2: {
                                  maxLength: 10
                   }
               },
     "TableB": {
                   property1: { 
                                  maxLength: 10
                              }
                   property2: {
                                  maxLength: 10
                   }
                }
    }

*/
// I want to look for some method similar to this
var allEntityTypeConfiguration = entityFrameworkConfig.getAllEntityTypeConfiguration()

毕竟,我想要检索的是EntityTypeConfiguration及其配置中定义的所有属性的列表(e.x:maxLength 10,或类似的东西)

我用谷歌搜索了这个问题一段时间,但仍未找到EF提供的任何方法。如果可能,是否有解决方法?

1 个答案:

答案 0 :(得分:1)

工作代码

     using (var dbContext = new YourDBEntities())
        {
            var metadata = ((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace;

            var tables = metadata.GetItemCollection(DataSpace.SSpace)
                .GetItems<EntityContainer>()
                .Single()
                .BaseEntitySets
                .OfType<EntitySet>()
                .Where(s => !s.MetadataProperties.Contains("Type")
                || s.MetadataProperties["Type"].ToString() == "Tables");

           //tables will be having list of table in your entity framework

            foreach (EntitySet table in tables) //foreach on tables list
            {
                //if you need table name
                var tableName = table.MetadataProperties.Contains("Table")
                    && table.MetadataProperties["Table"].Value != null
                    ? table.MetadataProperties["Table"].Value.ToString()
                    : table.Name;

               //if you need schema name
                var tableSchema = table.MetadataProperties["Schema"].Value.ToString();

               //Here are the list of complete columns in that table
                var columnDetails = table.ElementType.Members;

                //you can get each column properties from above columns list             
            }
        }