0
votes

I have two tables in my SQL Server database - one for categories:

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; }
}

And then the Assistant table:

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; }
}

When I try to make a migration I get this error:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Assistants_Categories_CategoryId". The conflict occurred in database "Pirma isMsSql", table "dbo.Categories", column 'Id'.

I have no idea why.

Thanks

1
Because a value isn't in the table, dbo.Categories in the column Id. - Larnu
New DB, I'm only designing DB. Not sure what value you mean. @Larnu - lokiys
At least one in the column you are adding the foreign key to. You can't create a foreign key if the data already breaks said constraint. The error is telling you the problem. - Larnu
It works well in my project.I think you need first remove previoud migrations and then add migration. - Rena

1 Answers

0
votes

Fix your classes:


 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; }
    }

This is db context:


 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);
    }