vendredi 10 mars 2017

Entity Framework model creating with reflection

I'm trying to create a model with classes derived in different assemblies. When I do this without reflection, evething is OK: migration is being created successfully and database is being updated.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<DerivedClass>();
}

When I use the reflection, migration is being created without any changes.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");

    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        var entityTypes = assembly
          .GetTypes()
          .Where(t =>
            t.BaseType == typeof(GameOperation));
        foreach (var type in entityTypes)
        {
            entityMethod.MakeGenericMethod(type)
                .Invoke(modelBuilder, new object[] { });
        }
    }
}

But when I'm launching application in debug mode, I see that entity adds to modelBuilder! And application says that

The model backing the 'EFDbContext' context has changed since the database was created





Aucun commentaire:

Enregistrer un commentaire