简体   繁体   中英

Entity Framework Core: Detect changes in model

I have 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:

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. So, ".ChangeTracker.HasChanges()" doesn't allow me to do this. 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:

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. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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