简体   繁体   English

C#-Dll插件和实体框架

[英]C# - Dll-Plugin and Entity-Framework

I am using a PluginManager which searches for dll Files and add them (and her functions) to my current application. 我正在使用一个PluginManager来搜索dll文件,并将它们(及其功能)添加到当前应用程序中。

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. 我还使用实体框架从对象创建数据库和表,但是必须在DatabaseContext类中声明数据库的对象。

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 EntityFramework.dll中发生了类型为'System.InvalidOperationException'的未处理异常

Additional information: The model backing the 'DatabaseContext' context has changed since the database was created. 附加信息:自创建数据库以来,支持“ DatabaseContext”上下文的模型已更改。 Consider using Code First Migrations to update the database ( http://go.microsoft.com/fwlink/?LinkId=238269 ). 考虑使用Code First Migrations更新数据库( http://go.microsoft.com/fwlink/?LinkId=238269 )。

EF can support adding dynamic DBSets using this following example . EF可以使用以下示例支持添加动态DBSet。

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. 这个想法是,您可以重载OnModelCreating方法,而不是在Db Context上手动创建DbSet,该方法允许动态构建类型。

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. 请记住,此示例采用了自定义属性PersistentAttribute ,该属性用于注释类,以便此代码找出适合模型生成的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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