代码之家  ›  专栏  ›  技术社区  ›  jwatts1980

为什么EF说我的模型已经改变了,而它没有?

  •  2
  • jwatts1980  · 技术社区  · 7 年前

    系统InvalidOperationException:支持“McContext”的模型 自创建数据库以来,上下文已更改。考虑使用 ( http://go.microsoft.com/fwlink/?LinkId=238269 ).

    我正在创建一个。NET MVC 5网站,使用nuget v6.1.3的最新实体框架。我附加到本地MDF文件,因为我没有在本地安装SQL Server或Express。我使用的是代码优先模型。

    我昨天和今天花了几个小时梳理了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"));
        }   
    }
    

    娱乐步骤:

    1. 我从头开始,删除了测试数据库中的所有数据库表,并删除了所有数据迁移。
    2. 重建解决方案。(基础项目和单元测试项目)
    3. 在Package Manager控制台中,运行“ Add-Migration StartupConfig
    4. 将迁移文件添加到数据\迁移中,称为“ {datetime}_StartupConfig.cs StartupConfig 以上等级。
    5. 我建造了这个项目。
    6. Update-Database -TargetMigration:StartupConfig "
    7. __MigrationHistory “和” Families “表已添加到数据库中,每个表有1条记录。

    我有一个更复杂的模型,但我开始一个接一个地删除,使事情尽可能简单。但是我没有比这更简单的了,但是错误仍然存在。值得注意的是,对于这个特定的项目,我从未从单元测试中获得过成功的查询。

    当我在上面输入这些娱乐步骤时,我同时执行这些步骤,得到了同样的错误。

    我不知道下一步该怎么办?

    2 回复  |  直到 7 年前
        1
  •  1
  •   EduLopez    7 年前

    由于您的代码没有改变,它必须与迁移表(包含您的数据库模型信息的表)或您正在使用的数据库文件有关。

    尝试查看您的项目正在使用的实例的名称。默认情况下,EF使用DefaultConnection。查看web。所有项目中的配置文件。

        2
  •  1
  •   jwatts1980    7 年前

    事实证明,这个应用程序。单元测试的配置没有连接字符串,并且将“sqlTestConnection”视为数据库名称,因为它无法以其他方式解析它。

    正在将连接字符串添加到应用程序。配置解决了该问题。