0

I am using a PluginManager which searches for dll Files and add them (and her functions) to my current application.

I also use Entity Framework to create a database and tables from objects, but I have to declare the objects for the database in the DatabaseContext class.

How can I save some objects from a plugin in this database?

I tried this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        foreach( PluginDto plugin in BackendContext.Current.PluginManager._plugins) {
            foreach(Type obj in plugin.plugin.getPluginDatabaseObjects())
            {
                Type typ = typeof(EntityTypeConfiguration<>).MakeGenericType(obj);

                List<MethodInfo> l = modelBuilder.GetType().GetMethods().ToList<MethodInfo>();

                MethodInfo m_Entitiy = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(new Type[] { obj });
                var configObj = m_Entitiy.Invoke(modelBuilder, null);

                MethodInfo m_ToTable = configObj.GetType().GetMethod("ToTable", new Type[] { typeof(String) });
                m_ToTable.Invoke(configObj, new object [] { obj.Name });
            }
        }
    }

But get this exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

Additional information: The model backing the 'DatabaseContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Flo
  • 1,179
  • 3
  • 15
  • 43
  • Did you look at [this](http://stackoverflow.com/questions/5308336/how-can-i-create-dbsett-dynamically-in-entity-framework)? – Benjamin Soulier Sep 05 '16 at 13:10
  • @bsoulier I have tried this: And get this error: "The entity type Field is not part of the model for the current context." – Flo Sep 05 '16 at 14:14
  • Seems taht EF can't really do dynamic tables according to this error, check [here](http://stackoverflow.com/questions/20688922/the-entity-type-type-is-not-part-of-the-model-for-the-current-context) – Benjamin Soulier Sep 06 '16 at 05:57
  • @bsoulier i think you are right! Do you know a good alternative that can handle this? – Flo Sep 06 '16 at 07:55

1 Answers1

0

EF can support adding dynamic DBSets using this following example.

The idea is that, instead of manually creating your DbSets on your Db Context, you overload the OnModelCreating method, which allows to build types on the fly.

Bear in mind that ths sample takes a custom attribute PersistentAttribute, used to annotate classes in order for this code to find out which classes fits into model generation.

Benjamin Soulier
  • 2,223
  • 1
  • 18
  • 30
  • This working fine but when i add a new class it fails – Flo Sep 06 '16 at 08:26
  • This has to do with EF Code first having to update its tables to persist the model, don't forget to update your model every time you add/update entities as shown at the bottom of the article – Benjamin Soulier Sep 06 '16 at 08:29
  • I tryed this! But i not get it to work... Can you give me a small example? – Flo Sep 06 '16 at 09:51