efcore icon indicating copy to clipboard operation
efcore copied to clipboard

DbModelBuilder Types method

Open giancarloa opened this issue 10 years ago • 2 comments

Hi... in EF6 I had the code below that would apply generic configuration to all entities in the model using the Types().Configure method... I am trying to port to EF7 and am struggling trying to convert this code... I see that we no longer have DbModelBuilder and instead have ModelBuilder... that's cool... but I can't find the Types method... is there anything equivalent in EF7???? I basically want to loop through all the types in the model, check if they derive from a certain base class, and if so ignore a prop on the base class... down below DerivesFrom is a custom extension method and EntityBase is custom base class... any ideas??? Thank you

protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder);

        modelBuilder.Types().Configure(entity =>
        {
            var isEntity = entity.ClrType.DerivesFrom(typeof(EntityBase<>));
            if (isEntity)
            {
                entity.Ignore("EntityState");
            }
        });
    }

giancarloa avatar Jan 29 '16 17:01 giancarloa

modelBuilder.Model.GetEntityTypes() returns all entity types in the current model. Following code snippet should be equivalent of above code in EF6.

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
    if (typeof (EntityBase<>).IsAssignableFrom(entityType.ClrType))
    {
        modelBuilder.Entity(entityType.ClrType).Ignore("EntityState");
    }
}

smitpatel avatar Jan 29 '16 18:01 smitpatel

wow thanks @smitpatel... i will try it out...

giancarloa avatar Jan 29 '16 19:01 giancarloa