简体   繁体   中英

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.

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:

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,

So I have convert [ef core code] to [ef 6] ?

Not sure what do you mean by shadow properties, but for the sample scenario EF6 provides a simpler DbModelBuilder.Types<T> method:

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:

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.

Also, if you can't find a better solution you can always make a base class that implements IAuditable and configure it with attributes.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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