繁体   English   中英

EF Core从多重性为零或一的模型中获取实体的导航属性

[英]EF Core get Navigation Properties of an entity from Model with multiplicity Zero or One

我正在创建一个通用类,以使用集成测试所需的实体来为数据库添加种子。 我可以创建单个实体,但是当一个实体彼此依赖时,我必须手动指示班级首先创建父实体,然后继续。 我试图使这种检测自动进行,从模型定义中获取多重性为0或1的导航属性列表(参考导航属性),一旦完成,递归地我的类将首先调用自身以创建父实体(循环依赖)在此超出范围)。

我曾经在.net Framework EF中执行此操作,但EF Core发生了很大变化。 我在EF Core中缺少的是RelationshipMultiplicity ,我在官方文档中找不到任何对Multiplicity的引用,甚至棘手的解决方案是检查导航属性是否为集合,我想拥有更多控制权并保留所有东西简单。

到目前为止,我正在使用以下方法探索模型定义:

var modelData = _context.Model.GetEntityTypes()
    .Select(t => new
    {
        t.ClrType.Name,
        DerivedNavigationProperties = t.FindDerivedNavigations(t.ClrType.Name),
        DefiningNavigationProperties = t.FindDefiningNavigation(),
        DeclaredForeignKeys = t.GetDeclaredForeignKeys(),
        DeclaredNavigations = t.GetDeclaredNavigations(),
        DerivedNavigations = t.GetDerivedNavigations(),
        DerivedNavigationsInclusive = t.GetDerivedNavigationsInclusive(),
        Navigations = t.GetNavigations() // This returns all Navigation Properties (INavigation)
    });

在检查了GitHub中的源代码之后,我可以很有把握地说,EF Core中没有诸如Multiplicity之类的东西。

我创建了一个枚举,类似于使用的.net Framework 3.5+(请参阅: 官方文档 ):

public enum RelationshipMultiplicity
{
    Many = 2,
    One = 1,
    ZeroOrOne = 0
}

然后是扩展方法,该方法允许使用枚举作为过滤器来获取所有导航属性。 我使用的关键是:

该方法允许按关系类型获取所有Navigations属性

public static class ModelExtensions
{
    /// <summary>
    /// Extension method used to get from the entity all navigation properties by multiplicity
    /// </summary>
    /// <typeparam name="T">Entity from where the navigation properties are taken</typeparam>
    /// <param name="model">Context Model</param>
    /// <param name="multiplicity">Type of multiplicity to use</param>
    /// <returns>List of PropertyInfo of Navigation Properties</returns>
    public static IEnumerable<PropertyInfo> GetNavigationProperties<T>(this IModel model, RelationshipMultiplicity multiplicity)
    {
        var navigations = model.GetEntityTypes().FirstOrDefault(m => m.ClrType == typeof(T))?.GetNavigations();
        var properties = new List<PropertyInfo>();

        switch (multiplicity)
        {
            case RelationshipMultiplicity.Many | RelationshipMultiplicity.ZeroOrOne:
                return navigations?
                    .Select(nav => nav.PropertyInfo);
            case RelationshipMultiplicity.Many:
                return navigations?
                    .Where(nav => nav.IsCollection())
                    .Select(nav => nav.PropertyInfo);
            case RelationshipMultiplicity.One:
                return navigations?
                    .Where(nav => !nav.IsCollection() && nav.ForeignKey.IsRequired)
                    .Select(nav => nav.PropertyInfo);
            case RelationshipMultiplicity.ZeroOrOne:
                return navigations?
                    .Where(nav => !nav.IsCollection())
                    .Select(nav => nav.PropertyInfo);
            default:
                return null;
        }

        return properties;
    }
}

用法示例:

var oneToManyRelations = _context.Model.GetNavigationProperties<Transaction>(
    RelationshipMultiplicity.ZeroOrOne);

var manyToOneRelations = _context.Model.GetNavigationProperties<Transaction>(
    RelationshipMultiplicity.Many);

var allRelations = _context.Model.GetNavigationProperties<Transaction>(
    RelationshipMultiplicity.Many | 
    RelationshipMultiplicity.ZeroOrOne);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM