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).

4
  • Did you look at this? Commented Sep 5, 2016 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." Commented Sep 5, 2016 at 14:14
  • Seems taht EF can't really do dynamic tables according to this error, check here Commented Sep 6, 2016 at 5:57
  • @bsoulier i think you are right! Do you know a good alternative that can handle this? Commented Sep 6, 2016 at 7:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This working fine but when i add a new class it fails
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
I tryed this! But i not get it to work... Can you give me a small example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.