【问题标题】:How to get the reference to all Entity Framework EntityTypeConfiguration如何获取对所有实体框架 EntityTypeConfiguration 的引用
【发布时间】:2018-02-14 23:36:41
【问题描述】:

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

假设我在 2 个类模型 A、B 上配置了这 2 个实体映射: 类 A { 字符串属性 1 { 获取;设置 }, 字符串属性 2 {get;放} } B 类 { 字符串属性 1 { 获取;设置 }, 字符串属性 2 {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 的配置,并且对 DB 的读写工作正常。

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

//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 中定义的所有属性的列表及其配置(例如:maxLength 10,或类似的东西)

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

【问题讨论】:

  • 嘿@Son Do,当你说所有属性意味着......你想要检索所有 表名列名 ** 与 **数据类型 哪些存在于 EF 中,对吗?
  • 嗨,你说得对
  • 请查看以下解决方案,如果您觉得它有用,请接受它作为答案

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


【解决方案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             
            }
        }

【讨论】:

  • 感谢您的解决方案。让我用我的代码测试一下,我会回复你的 :)
猜你喜欢
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
相关资源
最近更新 更多