tôi cố gắng triển khai Thực thể khung thành dự án của tôi! Dự án của tôi là dựa trên plugin vì vậy tôi không biết đối tượng nào tôi phải lưu vào cơ sở dữ liệu.Cập nhật tự động khung thực thể
Tôi đã thực hiện nó như vậy:
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);
}
}
Nhưng tôi nhận được ngoại lệ này, khi tôi đưa ra một sự thay đổi:
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).
Lỗi này là hoàn toàn logic. Cơ sở dữ liệu không đồng bộ, nhưng làm thế nào tôi sẽ nhận được bản cập nhật? Tôi đã đọc điều gì đó về điều này:
var config = new DbMigrationsConfiguration<MyContext> { AutomaticMigrationsEnabled = true };
var migrator = new DbMigrator(config);
migrator.Update();
Nhưng tôi không biết cách sử dụng và cách sử dụng đúng cách! Cảm ơn bạn rất nhiều!
EDIT1: Khi tôi cố gắng để: Enable-Migrations -EnableAutomaticMigrations
Tôi đã nhận lỗi này:
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)
EDIT2:
<connectionStrings>
<add name="DatabaseContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=SOM_db.sdf;Max Database Size=1024" />
</connectionStrings>
Có vẻ như bạn đang sử dụng công cụ sai cho lớp kiên trì của mình. Bạn có nghĩ về giải pháp NoSQL không? –
@raderick Đây không phải là một lựa chọn .... – Flo
Bạn đã thử cập nhật tought bảng điều khiển quản lý gói PowerShell hoặc nuget, sử dụng Update-Database -Verbose chưa? Ngoài ra, liệu tham chiếu null có trỏ đến bảng mới được tạo bởi quá trình di chuyển không? – GaelSa