简体   繁体   English

Entity Framework Core:检测 model 中的变化

[英]Entity Framework Core: Detect changes in model

I have model我有 model

public class BBDB
{
    [Key]
    [ForeignKey("Person")]
    public int Id { get; set; }

    [CanBeNull]
    public string City { get; set; }
}

And added new property.并添加了新属性。 My model become:我的 model 变成:

public class BBDB
{
    [Key]
    [ForeignKey("Person")]
    public int Id { get; set; }

    [CanBeNull]
    public string City { get; set; }

    [NotNull]
    public string Date { get; set; }
}

I need to check using dbcontext that this changes exist.我需要使用 dbcontext 检查是否存在此更改。 So, ".ChangeTracker.HasChanges()" doesn't allow me to do this.所以,“.ChangeTracker.HasChanges()”不允许我这样做。 Add-migration create code to make changes.添加迁移创建代码以进行更改。 How can I check that we have changes?如何检查我们是否有更改?

Ok It is very easy in the end...好的最后很容易......

Context.Database.Initialize

This solve our problem If there are changes in model that we don't migrate, it crashes with message:这解决了我们的问题如果 model 中有我们不迁移的更改,它会崩溃并显示消息:

System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException:无法更新数据库以匹配当前的 model,因为有待处理的更改并且自动迁移被禁用。 Either write the pending model changes to a code-based migration or enable automatic migration.将挂起的 model 更改写入基于代码的迁移或启用自动迁移。 Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 以启用自动迁移

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM