简体   繁体   English

实体框架6代码首先处理阴影属性

[英]Entity Framework 6 code first handing shadow properties

I know how to do handing shadow properties in entity framework core 我知道如何在实体框架核心中处理阴影属性

I thought that will be same in entity framework 6. 我认为在实体框架6中将是相同的。

It turns out not same as I thought. 结果与我的想法不一样。

Basically I have a interface named: 基本上我有一个名为的接口:

public interface IAuditable { }

It is pretty sample do it on OnModelCreating function in EF core: 在EF核心中的OnModelCreating函数上做它很漂亮:

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            if (typeof(IAuditable).IsAssignableFrom(entityType.ClrType))
            {
                modelBuilder.Entity(entityType.ClrType).Property(typeof(string), Auditable.CreatedBy).HasMaxLength(50).IsRequired();
                modelBuilder.Entity(entityType.ClrType).Property(typeof(string), Auditable.UpdatedBy).HasMaxLength(50).IsRequired();
                modelBuilder.Entity(entityType.ClrType).Property(typeof(DateTime), Auditable.CreatedOn).IsRequired();
                modelBuilder.Entity(entityType.ClrType).Property(typeof(DateTime), Auditable.UpdatedOn).IsRequired();
            }
        }

But EF 6 don't have modelBuilder.Model property, 但是EF 6没有modelBuilder.Model属性,

So I have convert [ef core code] to [ef 6] ? 所以我将[ef核心代码]转换为[ef 6]?

Not sure what do you mean by shadow properties, but for the sample scenario EF6 provides a simpler DbModelBuilder.Types<T> method: 不确定shadow属性是什么意思,但是对于示例场景, DbModelBuilder.Types<T>提供了一个更简单的DbModelBuilder.Types<T>方法:

Begins configuration of a lightweight convention that applies to all entities and complex types in the model that inherit from or implement the type specified by the generic argument. 开始轻量级约定的配置,该约定适用于模型中继承或实现泛型参数指定的类型的所有实体和复杂类型。 This method does not register types as part of the model. 此方法不会将类型注册为模型的一部分。

The equivalent EF6 code is something like this: 等效的EF6代码是这样的:

modelBuilder.Types<IAuditable>().Configure(e =>
{
    e.Property(Auditable.CreatedBy).HasMaxLength(50).IsRequired();
    e.Property(Auditable.UpdatedBy).HasMaxLength(50).IsRequired();
    e.Property(Auditable.CreatedOn).IsRequired();
    e.Property(Auditable.UpdatedOn).IsRequired();
});

I don't know if there is a more simple method. 我不知道是否有更简单的方法。 Actually, to retrieve all the entity types I scan the context looking for DbSets. 实际上,要检索所有实体类型,我会扫描上下文以查找DbSet。

Also, if you can't find a better solution you can always make a base class that implements IAuditable and configure it with attributes. 此外,如果找不到更好的解决方案,则可以始终创建一个实现IAuditable的基类,并使用属性对其进行配置。

Anyway, here the code I use to scan the context: 无论如何,这里我用来扫描上下文的代码:

public void All()
{

    var properties = typeof (Context).GetProperties().Where(p => IsSubclassOfRawGeneric(typeof(DbSet<>), p.PropertyType));

    foreach (PropertyInfo property in properties)
    {
        Type entityType = property.PropertyType.GetGenericArguments()[0];
        // ...
    }

}


static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)
{
    while (toCheck != null && toCheck != typeof(object))
    {
        var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
        if (generic == cur)
        {
            return true;
        }
        toCheck = toCheck.BaseType;
    }
    return false;
}

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

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