我不断收到以下错误:为什么EF说我的模型已经改变了呢?
System.InvalidOperationException: The model backing the 'McContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
我使用的是最新的实体框架从的NuGet,v6.1.3创建.NET MVC 5网站。我连接到本地MDF文件,因为我没有在本地安装SQL Server或Express。我正在使用Code First模型。
我昨天和今天花了几个小时来梳理SO和WWW上的这个错误帖子,但还没有找到可行的解决方案。它们大多数都是围绕不同类型的初始化程序进行的,或者是有关代码首先需要迁移或初始化程序以进行模型更改的简短说明。但我通过PM控制台手动运行设置。我在下面列出了我的步骤。
连接字符串:
<add name="testSQLConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\jwatts\Documents\Visual Studio 2015\Projects\MyProject\Databases\MyProject01.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
模型:
public class Family
{
public Family() { }
[Key, Index(IsUnique = true)]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[StringLength(128)]
public string Name { get; set; }
[StringLength(1024)]
public string Description { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime? Changed { get; set; }
public string ChangedBy { get; set; }
public DateTime? Deleted { get; set; }
public string DeletedBy { get; set; }
}
语境:
public class McContext : DbContext
{
public McContext() : base("testSQLConnection") { }
public DbSet<Family> Families { get; set; }
}
配置:
internal sealed class Configuration : DbMigrationsConfiguration<Data.McContext>
{
public Configuration()
{
MigrationsDirectory = @"Data\Migrations";
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;
}
protected override void Seed(McContext context)
{
context.Families.Add(new Family()
{
Name = "My Family",
Description = "The family",
Created = DateTime.Now,
CreatedBy = "system"
});
context.SaveChanges();
}
}
自动生成的迁移类:
public partial class StartupConfig : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Families",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 128),
Description = c.String(maxLength: 1024),
Created = c.DateTime(nullable: false),
CreatedBy = c.String(),
Changed = c.DateTime(),
ChangedBy = c.String(),
Deleted = c.DateTime(),
DeletedBy = c.String(),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Id, unique: true);
}
public override void Down()
{
DropIndex("dbo.Families", new[] { "Id" });
DropTable("dbo.Families");
}
}
单元测试代码:
[TestMethod]
public void CreateTheContext_Test()
{
using (var ctx = new McContext())
{
//error on this line below:
var fam = ctx.Families.Where(f => f.Name.Contains("My")).FirstOrDefault();
Assert.IsTrue(fam != null && fam.Name.Contains("My"));
}
}
娱乐步骤:
- 我开始用一个 “清白”,并删除了所有的数据库表在我的测试数据库,并删除所有Data \ Migrations。
- 重建解决方案。 (基础项目和一个单元测试项目)
- 在包管理控制台,然“
Add-Migration StartupConfig
” - 迁移文件被添加到数据\迁移称为“
{datetime}_StartupConfig.cs
”。见上面的StartupConfig
类。 - 我建立了这个项目。
- 我跑了“
Update-Database -TargetMigration:StartupConfig
” - PM控制台输出4行,包括“运行种子方法”,并成功完成。
- 我通过SQL查询确认已将“
__MigrationHistory
”和“Families
”表添加到数据库,并且每个表都有1条记录。 - 我跑了单元测试并收到错误。
我有一个更复杂的模型,但我开始一件一件地把东西做得尽可能简单。但我不能比这更简单,但错误仍然存在。值得注意的是,对于这个特定的项目,我从来没有从单元测试中获得成功的查询。
当我输入上述这些娱乐步骤时,我在同一时间执行这些步骤,并得到相同的错误。
我不知道下一步该怎么办?
将断点放入具有连接字符串的上下文中。验证单元测试是否将连接传递给相同的实例名称和数据库名称。单元测试不会自动使用主启动项目中的web.config。 – AaronLS
@AaronLS这很奇怪.. ctx.Database.Connection的监视窗口中的连接字符串是'“Data Source =(localdb)\\ mssqllocaldb; Initial Catalog = testSQLConnection; Integrated Security = True; MultipleActiveResultSets = True”' – jwatts1980
这就是甚至没有连接字符串,在我的应用程序任何地方..它从哪里得到?迁移正在处理连接正常。但是,当单元测试实例化上下文时,它会得到这个奇怪的连接字符串。对可能造成这种情况的任何想法? – jwatts1980