我尝试将Entity-Framework引入我的项目中!我的项目是基于插件的,因此我不知道要保存哪个对象到数据库中。
我已经这样实现:
public class DatabaseContext : DbContext
{
public DatabaseContext() : base()
{
Database.Initialize(true);
}
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 });
}
}
base.OnModelCreating(modelBuilder);
}
}
但是,当我进行更改时,出现了以下异常:
'DatabaseContext'上下文的支持模型已自数据库创建以来发生更改。考虑使用Code First Migrations更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
这个错误是完全合乎逻辑的。数据库不同步,但我将如何获得更新?我已阅读了一些相关内容:
var config = new DbMigrationsConfiguration<MyContext> { AutomaticMigrationsEnabled = true };
var migrator = new DbMigrator(config);
migrator.Update();
但我不知道如何正确使用它以及在哪里使用它!非常感谢!
编辑1: 当我尝试使用以下命令:Enable-Migrations –EnableAutomaticMigrations
我得到了这个错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at SOM.Backend.database.DatabaseContext.OnModelCreating(DbModelBuilder modelBuilder) in C:\Users\Flo\Documents\Visual Studio 2015\Projects\SOM\Backend\BackendService\BackendService\database\DatabaseContext.cs:line 26
at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.MarkDatabaseInitialized()
at System.Data.Entity.Database.Initialize(Boolean force)
at SOM.Backend.database.DatabaseContext..ctor() in C:\Users\Flo\Documents\Visual Studio 2015\Projects\SOM\Backend\BackendService\BackendService\database\DatabaseContext.cs:line 21
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
编辑2:
<connectionStrings>
<add name="DatabaseContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=SOM_db.sdf;Max Database Size=1024" />
</connectionStrings>