错误信息:

“自数据库创建以来,支持‘AddressBook’上下文的模型已经发生了变化。手动删除/更新数据库,或者调用database。带有IDatabaseInitializer实例的SetInitializer。例如,RecreateDatabaseIfModelChanges策略将自动删除并重新创建数据库,并可选地为其添加新数据。”

我正在尝试使用代码优先功能,以下是我写的:

var modelBuilder = new ModelBuilder();
var model = modelBuilder.CreateModel();
using (AddressBook context = new AddressBook(model))
{
    var contact = new Contact
    {
        ContactID = 10000,
        FirstName = "Brian",
        LastName = "Lara",
        ModifiedDate = DateTime.Now,
        AddDate = DateTime.Now,
        Title = "Mr."

    };
    context.contacts.Add(contact);
    int result = context.SaveChanges();
    Console.WriteLine("Result :- "+ result.ToString());
}

上下文类:

public class AddressBook : DbContext
{
    public AddressBook()
    { }
    public AddressBook(DbModel AddressBook)
        : base(AddressBook)
    {

    }
    public DbSet<Contact> contacts { get; set; }
    public DbSet<Address> Addresses { get; set; }
}

和连接字符串:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
    <add name="AddressBook" providerName="System.Data.SqlClient"  
         connectionString="Data Source=MyMachine;Initial Catalog=AddressBook;
         Integrated Security=True;MultipleActiveResultSets=True;"/>
    </connectionStrings>
</configuration>

因此,数据库名称是“AddressBook”,当我试图将联系人对象添加到上下文时发生错误。我遗漏了什么吗?


当前回答

我花了很多天来解决这个问题,分析了很多不同的帖子,尝试了很多选择,最终解决了这个问题。 这2个项目在我的解决方案使用EF代码第一次迁移:

控制台应用程序“DataModel”,主要用作程序集,其中包含我所有的代码第一个实体,DbContext, Mirgations和通用存储库。我在这个项目中包含了单独的空本地数据库文件(在DataModel/App_Data文件夹中),以便能够从包管理器控制台生成迁移。 WebApi引用了DataModel项目,并使用了WebApi/App_Data文件夹中的本地数据库文件,该文件不包含在项目中

当请求WebApi时,我得到了这个错误…

我的环境:

Windows 8.1 x64 Visual Studio 2015 Professional with Update 1 我所有的项目都针对。net Framework 4.6.1 EntityFramework 6.1.3 from NuGet

在这里,我收集了所有你应该注意的事项和所有必须满足的条件/要求,以避免提到的例外:

You should use only one version of EntityFramework Nuget package for all projects in your solution. Database, created by running sequentially all migration scripts should have the same structure/schema as you target database and correspond to entity model. Following 3 things must exactly correspond/reflect/match each other: Your all migration script up to last Current code first entity model state (DbContext, entities) Target database Target database (mdf file) should be updated/correspond up to last migration script. Verify that "__MigrationHistory" table in your target database contains records for all migration scripts that you have, it means that all migration scripts was successfully applied to that database. I recommend you to use Visual Studio for generation correct code first entities and context that corresponds to your database, Project -> Add New Item -> ADO.NET Entity Data Model -> Code First from database: Of course, as an alternative, if you have no database you can write manually model (code first entities and context) and then generate initial migration and database. Name of connection string e.g. MyConnectionString in config file of startup project (Web.config/App.config): <configuration> <connectionStrings> <add name="MyConnectionString" connectionString="..."> </connectionStrings> <configuration> should be equal to parameter passed in constructor of your DbContext: public partial class MyDbContext : DbContext { public MyDbContext() : base("name=MyConnectionString"){} ... Before using Package Manager Console, make sure that you are using correct database for update or generate migration and needed project is set as startup project of solution. For connect to database it will use connection string from that .config file, which in project, that is set as startup project. And the main, which fixed my issue: It is weird, but in my WebApi/bin folder DataModel.exe was old, not refreshed since last build. Since migrations was embedded in my assembly DataModel.exe then my WebApi updated database using old mirgations. I was confused why after updating database in WebApi it not corresponds to latest migration script from DataModel. Following code automatically creates(if not exists) or updates to latest migration local database in my WebApi/App_Data folder. public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<ODS_DbContext, Configuration>()); ... I tried clean and rebuild solution but it did not help, than I completely removed bin and obj folders from WebApi, deleted database files from WebApi/App_Data, built, restarted WebApi, made request to it, it created correct database - lazy initialization (using lines above), which corresponds to latest migration and exception didn't appear more. So, this may fix your problem: remove manually bin, obj folders from your startup project (which generates/updates your database) build your startup project or better clean and rebuild all you solution. recreate database by starting project (will execute lines above) or use Package Manager Console "update-database" command. manually check whether generated db and __MirgationHistory corresponds to latest migration script.

其他回答

以防有人遇到和我一样的情况。

我有数据库首先EF,同时使用asp.net身份

所以我在我的webconfig中有两个connectionStrings,这没有问题。碰巧我创建/运行脚本手动生成asp.net标识表,我不应该这样做。

所以首先DROP所有的asp.net标识表,由你手工创建/从脚本。

DROP TABLE __MigrationHistory
DROP TABLE AspNetRoles
DROP TABLE AspNetUserClaims
DROP TABLE AspNetUserLogins
DROP TABLE AspNetUserRoles
DROP TABLE AspNetUsers

此修复在CTP5之后不再有效。

你必须执行database。setinitializer <YourContext>(null);

I am reading the Pro ASP.NET MVC 4 book as well, and ran into the same problem you were having. For me, I started having the problem after making the changes prescribed in the 'Adding Model Validation' section of the book. The way I resolved the problem is by moving my database from the localdb to the full-blown SQL Server 2012 server. (BTW, I know that I am lucky I could switch to the full-blown version, so don't hate me. ;-))) There must be something with the communication to the db that is causing the problem.

好建议,但并非在所有情况下都如此准确。我算出一个。 请确保您在Visual Studio中使用PM窗口运行“enable-migrations”,迁移文件夹将添加到您的项目中。

确保添加到文件夹中的两个c#类文件将包含所有模型及其各自的属性。

如果您拥有所有这些,就构建解决方案,并发布部署。

逻辑是不能覆盖现有元数据,因为应用程序没有元数据来替换当前元数据。结果,你会得到这样的错误“支持上下文的模型自数据库创建以来已经改变了”

检查以下步骤

Database.SetInitializer(空);——> Global.asax.cs

2.

你的Context类名应该与之匹配 检查它