如何在Entity Framework 4.3 Code First中禁用__MigrationHistory表的使用? [英] How can I disable the use of the __MigrationHistory table in Entity Framework 4.3 Code First?
问题描述
我正在使用一个自定义的数据库初始化程序,如下所示:
I'm using Entity Framework 4.3 Code First with a custom database initializer like this:
public class MyContext : DbContext
{
public MyContext()
{
Database.SetInitializer(new MyContextInitializer());
}
}
public class MyContextInitializer : CreateDatabaseIfNotExists<MyContext>
{
protected override void Seed(MyContext context)
{
// Add defaults to certain tables in the database
base.Seed(context);
}
}
每当我的模型发生变化,我编辑我的POCO和映射手动更新我的数据库。
Whenever my model changes, I edit my POCO's and mappings manually and I update my database manually.
当我再次运行我的应用程序时,我收到这个错误:
When I run my application again, I get this error:
'/'应用程序中的服务器错误。
自创建数据库以来,支持MyContext上下文的模型已更改。考虑使用代码优先迁移来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
说明: strong>在执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息及其在代码中的起始位置。
Server Error in '/' Application.
The model backing the 'MyContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
异常详细信息: System.InvalidOperationException:自创建数据库以来,支持MyContext上下文的模型已更改。考虑使用代码优先迁移来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
Exception Details: System.InvalidOperationException: The model backing the 'MyContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
使用EFProfiler,我也注意到执行这些查询:
Using EFProfiler, I also notice these queries being executed:
-- statement #1
SELECT [GroupBy1].[A1] AS [C1]
FROM (SELECT COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]) AS [GroupBy1]
-- statement #2
SELECT TOP (1) [Project1].[C1] AS [C1],
[Project1].[MigrationId] AS [MigrationId],
[Project1].[Model] AS [Model]
FROM (SELECT [Extent1].[MigrationId] AS [MigrationId],
[Extent1].[CreatedOn] AS [CreatedOn],
[Extent1].[Model] AS [Model],
1 AS [C1]
FROM [dbo].[__MigrationHistory] AS [Extent1]) AS [Project1]
ORDER BY [Project1].[CreatedOn] DESC
如何防止t他的?
推荐答案
起初我确定是因为你在ctor中设置了默认的初始化器,但调查了一下我发现当上下文创建时,初始化程序不会运行,而是第一次查询/添加某些内容时。
At first I was sure it was because you set the default initializer in the ctor but investigating a bit I found that the initializer isn't run when the context is created but rather when you query/add something for the first time.
提供的初始化程序全部检查模型的可压缩性,使您出来与他们的运气。您可以轻松地使用自己的初始化程序:
The provided initializer all check model compability so you are out of luck with them. You can easily make your own initializer like this instead though:
public class Initializer : IDatabaseInitializer<Context>
{
public void InitializeDatabase(Context context)
{
if (!context.Database.Exists())
{
context.Database.Create();
Seed(context);
context.SaveChanges();
}
}
private void Seed(Context context)
{
throw new NotImplementedException();
}
}
不应该检查可兼容性,如果数据库是缺少它会创建它。
That shouldn't check compability and if the Database is missing it will create it.
更新:上下文应该是DbContext的实现类型
UPDATE: "Context" should be the type of your implementation of DbContext
这篇关于如何在Entity Framework 4.3 Code First中禁用__MigrationHistory表的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!