【问题标题】:Migration conflicts in net core Entity Framework网络核心实体框架中的迁移冲突
【发布时间】:2021-05-12 11:44:09
【问题描述】:

我的 SQL Server 数据库中有两个表 - 一个用于类别:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? ParentId { get; set; }
    [ForeignKey("ParentId")]
    public Category Parent { get; set; }
}

然后是Assistant 表:

public class Assistant
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public DateTime CreatedAtUtc { get; set; }
    public Status Status { get; set; }
    public int? CategoryId { get; set; }
 
    [ForeignKey("CategoryId")]
    public Category Category { get; set; }
}

当我尝试进行迁移时出现此错误:

ALTER TABLE 语句与 FOREIGN KEY 约束“FK_Assistants_Categories_CategoryId”冲突。数据库“Pirma”中发生冲突 isMsSql”,表“dbo.Categories”,列“Id”。

我不知道为什么。

谢谢

【问题讨论】:

  • 因为表格中没有值,所以dbo.CategoriesId列中。
  • 新数据库,我只是设计数据库。不确定你的意思是什么价值。 @Larnu
  • 在您要添加外键的列中至少有一个。如果数据已经破坏了所述约束,则无法创建外键。错误告诉你问题所在。
  • 它在我的项目中运行良好。我认为您需要先删除以前的迁移,然后再添加迁移。

标签: sql-server entity-framework alter-table


【解决方案1】:

修复你的课程:


 public partial class Assistent
    {
       [Key]
       public int Id { get; set; }

       public int UserId { get; set; }
       public User User{ get; set; }

       public DateTime CreatedAtUtc { get; set; }

       public int StatusId { get; set; }
       public Status Status { get; set; }

        public int? CategoryId { get; set; }

        [ForeignKey(nameof(CategoryId))]
        [InverseProperty("Assistents")]
        public virtual Category Category { get; set; }
    }

     public partial class Category
    {
        [Key]
        public int Id { get; set; }
        
        public string Name { get; set; }
        public int? ParentId { get; set; }

         [ForeignKey(nameof(ParentId))]
        [InverseProperty(nameof(Category.InverseParent))]
        public virtual Category Parent { get; set; }

        [InverseProperty(nameof(Assistent.Category))]
        public virtual ICollection<Assistent> Assistents { get; set; }

        [InverseProperty(nameof(Category.Parent))]
        public virtual ICollection<Category> InverseParent { get; set; }
    }

这是数据库上下文:


 public partial class AssistentsContext : DbContext
    {
        public AssistentsContext()
        {
        }

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

        public virtual DbSet<Assistent> Assistents { get; set; }
        public virtual DbSet<Category> Categories { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            

            modelBuilder.Entity<Assistent>(entity =>
            {
               
                entity.HasOne(d => d.Category)
                    .WithMany(p => p.Assistents)
                    .HasForeignKey(d => d.CategoryId);
                   
            });

            modelBuilder.Entity<Category>(entity =>
            {
               
                     entity.HasOne(d => d.Parent)
                    .WithMany(p => p.InverseParent)
                    .HasForeignKey(d => d.ParentId);
                   
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 2021-06-27
    • 2021-09-01
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2019-11-08
    相关资源
    最近更新 更多