我如何以编程方式读取EF DbContext元数据?

时间:2013-03-30 12:03:12

标签: ef-code-first metadata entity-framework-5

我的应用程序使用EF-CodeFirst 5(dll ver 4.4.0.0,on .net 4.0)。

我需要能够读取实体元数据,以便我可以为给定的条目类型获取以下信息:

  • 哪些属性是一对多关系(引用实体)
  • 哪些属性是多个关系(引用当前属性的实体的集合)
  • 也很好但不是绝对必要的:哪些属性是很多关系(关系集合)

我可以通过在属性列表上编写foreach循环来获取此信息,然后通过依赖所有虚拟引用来“识别”它们,但我觉得这不是“正确”的方式。我知道EdmxWriter可以以xml格式提供该信息,但它通过访问不可公开访问的InternalContext来实现,我想直接获得强类型列表/数组,而不使用该xml。我应该使用哪种API(如果有这样的API,似乎我找不到它)?

1 个答案:

答案 0 :(得分:26)

Gorane,这应该让你开始......
(我没有玩过很多东西 - 需要在调试器中进行一些实验才能看到哪些属性/信息以及如何获取它)

using (var db = new MyDbContext())
{
    var objectContext = ((IObjectContextAdapter)db).ObjectContext;
    var container = objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace);
    foreach (var set in container.BaseEntitySets)
    {
        // set.ElementType.
        foreach (var metaproperty in set.MetadataProperties)
        {
            // metaproperty.
        }
    }

    // ...or... 

    var keyName = objectContext
        .MetadataWorkspace
        .GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
        .BaseEntitySets
        .First(meta => meta.ElementType.Name == "Question")
        .ElementType
        .KeyMembers
        .Select(k => k.Name)
        .FirstOrDefault();
}

更具体地......

foreach (var set in container.BaseEntitySets)
{
    var dependents = ((EntitySet)(set)).ForeignKeyDependents;
    var principals = ((EntitySet)(set)).ForeignKeyPrincipals;
    var navigationProperties = ((EntityType)(set.ElementType)).NavigationProperties;
    foreach (var nav in navigationProperties)
    {
        // nav.RelationshipType;
    }
}

其中一些属性似乎没有暴露给“一般公众”,所以你需要使用反射 - 或者找一些更聪明的方法 - 但是那里有很多信息。



这些链接还有更多信息......

How to get first EntityKey Name for an Entity in EF4

How can I extract the database table and column name for a property on an EF4 entity?


修改: 使用您的navigationProperties列表作为起点,我得到了我需要的一切:

        ManyToManyReferences = navigationProperties.Where(np =>
            np.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many &&
            np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            .Select(np => Extensions.CreateLambdaExpression<TEntity>(np.Name))
            .ToList();

        OneToManyReferences = navigationProperties.Where(np =>
            (np.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One ||
            np.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne) &&
            np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            .Select(np => Extensions.CreateLambdaExpression<TEntity>(np.Name))
            .ToList();

        ManyToOneReferences = navigationProperties.Where(np =>
            np.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many &&
            (np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One ||
            np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne))
            .Select(np => Extensions.CreateLambdaExpression<TEntity>(np.Name))
            .ToList();

        OneToOneReferences = navigationProperties.Where(np =>
            np.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One &&
            np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One)
            .Select(np => Extensions.CreateLambdaExpression<TEntity>(np.Name))
            .ToList();

CreateLambdaExpression方法不是我的礼貌,积分转到Jon Skeet,代码是在this answer

的帮助下创建的

这是我的CreateLambdaExpression方法:

public static Expression<Func<TEntity, object>> CreateLambdaExpression<TEntity>(string propertyName)
{
    ParameterExpression parameter = Expression.Parameter(typeof (TEntity), typeof (TEntity).Name);
    Expression property = Expression.Property(parameter, propertyName);

    return Expression.Lambda<Func<TEntity, object>>(property, new[] {parameter});
}