EntityFramework Core SavingChanges到数据库

时间:2018-03-21 08:05:38

标签: .net-core entity-framework-core

我正在从一个旧数据库到新数据库执行一些迁移数据。使用Fluent API使用EF Core Code-First创建新数据库。 所有表都成功迁移期望一个。在这张桌子上,当我想在数据库中保存一些更改时,我得到错误的字段"拒绝"为null,即使不为null。 description  该字段是整数类型。

我也做了一些SQL分析,我看到由于某些原因,Insert Into description

中缺少此字段

任何帮助以了解为什么会发生这种情况我将不胜感激。

更新

 public class Entries
    {
    public Guid Id { get; set; }

    public DateTime CreateDateTime { get; set; }

    public int CartonBoxId { get; set; }

    public CartonBoxes CartonBox { get; set; }

    public string Status { get; set; }

    public string UserId { get; set; }

    public ApplicationUser User { get; set; }

    public int Accepted { get; set; }

    public int Rejected { get; set; }

    public DateTime? UpdateDateTime { get; set; }

    public int MasterCodeId { get; set; }

    public MasterCodes MasterCode { get; set; }

    public int SubCodeId { get; set; }

    public SubCodes SubCode { get; set; }

    public int AqlId { get; set; }

    public Aqls Aql { get; set; }

}



public DbSet<Fashions> Fashions { get; set; }
        public DbSet<AqlVersions> AqlVersions { get; set; }
        public DbSet<Aqls> Aqls { get; set; }
        public DbSet<MasterCodes> MasterCodes { get; set; }
        public DbSet<SubCodes> SubCodes { get; set; }
        public DbSet<CartonBoxes> CartonBoxes { get; set; }
        public DbSet<Entries> Entries { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {

        builder.Entity<ApplicationUser>(entity =>
        {
            entity.HasMany(x => x.Entries)
                .WithOne(x => x.User)
                .HasForeignKey(x => x.UserId);
        });

        builder.Entity<Fashions>(entity =>
        {
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Id)
                .ValueGeneratedOnAdd();

            entity.Property(x => x.Name)
                .HasMaxLength(50)
                .IsRequired();

            entity.HasMany(x => x.Aqls)
                .WithOne(x => x.Fashion)
                .HasForeignKey(x => x.FashionId);
        });
        builder.Entity<AqlVersions>(entity =>
        {
            entity.Property(x => x.Id).ValueGeneratedOnAdd();
            entity.HasKey(x => x.Id);

            entity.Property(x => x.Name)
                .IsRequired()
                .HasMaxLength(50);

            entity.HasIndex(x => x.Name).IsUnique();

            entity.HasMany(x => x.Aqls)
                .WithOne(x => x.AqlVer)
                .HasForeignKey(x => x.AqlVerId);

        });

        builder.Entity<Aqls>(entity =>
        {
            entity.Property(x => x.Id).ValueGeneratedOnAdd();
            entity.HasKey(x => x.Id);

            entity.HasMany(x => x.Entries)
                .WithOne(x => x.Aql)
                .HasForeignKey(x => x.AqlId);

        });

        builder.Entity<MasterCodes>(entity =>
        {
            entity.Property(x => x.Id).ValueGeneratedOnAdd();
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Code)
                .IsRequired()
                .HasMaxLength(50);
            entity.Property(x => x.Eng)
                .HasMaxLength(50);
            entity.Property(x => x.Srb)
                .HasMaxLength(50);

            entity.HasMany(x => x.SubCodes)
                .WithOne(x => x.MasterCodes)
                .HasForeignKey(x => x.MasterCodeId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasMany(x => x.Entries)
                .WithOne(x => x.MasterCode)
                .HasForeignKey(x => x.MasterCodeId);

        });

        builder.Entity<SubCodes>(entity =>
        {
            entity.Property(x => x.Id).ValueGeneratedOnAdd();
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Code)
                .IsRequired()
                .HasMaxLength(50);
            entity.Property(x => x.Eng)
                .HasMaxLength(50);
            entity.Property(x => x.Srb)
                .HasMaxLength(50);

            entity.HasMany(x => x.Entries)
                .WithOne(x => x.SubCode)
                .HasForeignKey(x => x.SubCodeId)
                .OnDelete(DeleteBehavior.Restrict);
        });

        builder.Entity<CartonBoxes>(entity =>
        {
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Id).ValueGeneratedOnAdd();

            entity.Property(x => x.CartonBox)
                .IsRequired()
                .HasMaxLength(50);

            entity.Property(x => x.ManuallyStatus)
                .HasMaxLength(20);

            entity.Property(x => x.Sku)
                .HasMaxLength(20);

            entity.Property(x => x.Po)
                .HasMaxLength(50);

            entity.HasMany(x => x.Entries)
                .WithOne(x => x.CartonBox)
                .HasForeignKey(x => x.CartonBoxId);
        });

        builder.Entity<Entries>(entity =>
        {
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Id)
                .ValueGeneratedOnAdd();
            entity.Property(x => x.Status)
                .HasMaxLength(50);

            entity.Property(x => x.UpdateDateTime)
                .IsRequired(false);

            entity.Property(x => x.Accepted)
                .HasDefaultValue();

            entity.Property(x => x.Rejected)
                .HasDefaultValue();

            entity.Property(x => x.MasterCodeId)
                .HasDefaultValue(null);
            entity.Property(x => x.SubCodeId)
                .HasDefaultValue(null);

        });

        base.OnModelCreating(builder);

1 个答案:

答案 0 :(得分:2)

问题是HasDefaultValue

entity.Property(x => x.Rejected)
                .HasDefaultValue();

当属性值等于指定的默认值(在您的情况下为HasDefaultValue)时,EF Core不会包含在标有0的插入命令属性中。

我建议不要使用HasDefaultValue / HasDefaultValueSql,除非数据库表中的相应列具有默认表达式。如果您不想要类似的惊喜,请将其从其他属性流畅的配置中删除。

请参阅文档的Default Values部分。