实体框架 - 在启动时动态创建表

时间:2016-09-07 15:15:17

标签: c# entity-framework sql-server-ce

首先抱歉!我已经问了这个问题,但我想我必须更好地描述它。

我将用C#构建一个系统。系统加载从特定目录启动所有dll以扩展其功能。

我的愿望是我可以添加和删除dll,而不会导致系统崩溃!这工作正常。所以我想要一个插件系统。

问题:我想在实体框架中使用SQL Server CE数据库。

实体框架希望在开始时了解所有数据库对象。但由于dll的不同,我不知道它们。

我的想法:我为插件创建了一个界面来询问他们,他们想要将哪些对象保存在数据库中。

使用这种方法,我想给EF提供对象:

protected override void OnModelCreating(DbModelBuilder modelBuilder)

就像那样:

modelBuilder.Entity<Object>().ToTable("Object");

但后来我收到了这个错误:

  

自创建数据库以来,支持'DatabaseContext'上下文的模型已更改。考虑使用Code First Migrations来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。

然后我尝试通过以下方法解决此问题:

 PM> Enable-Migrations –EnableAutomaticMigrations

我通过以下方式初始化数据库:

public class DatabaseContext : DbContext
{
    public DatabaseContext() : base("DatabaseContext")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, Configuration>());
        Database.Initialize(true);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        PluginManager PluginManager = new PluginManager();
        PluginManager.loadPlugins();
        PluginManager.startPlugins();

        foreach ( PluginDto plugin in 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 });
            }
        }

        base.OnModelCreating(modelBuilder);
    }
}

然后我收到此错误:

  

类型'System.StackOverflowException'的未处理异常   发生在System.Data.SqlServerCe.dll

我该如何解决这个问题? EF应该可以吗?

你知道一些可以解决这个问题的替代框架吗?

0 个答案:

没有答案
相关问题