2

I have aproject with codefirst migrations wiht the following classes:

public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public virtual ICollection<Chat> Chats { get; set; }
    }

and

public class Chat

{
    [Key, Column(Order = 0)]
    public int User1Id { get; set; }

    public virtual User User1 { get; set; }

    [Key, Column(Order = 1)]
    public int User2Id { get; set; }

    public virtual User User2 { get; set; }

    public string Channel { get; set; }
}

The Idea is A chat has two users, and the chat Primary key is a composite key of the two user Ids, which are foreign keys.

A user has also a list of chats, in which he participates.

now when I try to run this, I get the following error:

Introducing FOREIGN KEY constraint 'FK_dbo.Chat_dbo.User_User2Id' on table 'Chat' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.

I kind of understand what's happening, but I don't know how to fix it. Tried this (from a topic here somewhere):

public DbSet<User> Users { get; set; }
        public DbSet<Chat> Chats { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Chat>()
                .HasRequired(s => s.User1)
                .WithMany()
                .WillCascadeOnDelete(true);

            modelBuilder.Entity<Chat>()
                .HasRequired(s => s.User2)
                .WithMany()
                .WillCascadeOnDelete(true);

            base.OnModelCreating(modelBuilder);
        }

but with no result, i still get the error. Can anyone shine some light on this matter?

Mario Stoilov
  • 3,411
  • 5
  • 31
  • 51

1 Answers1

1

try

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Chat> InitiatedChats { get; set; }
    public virtual ICollection<Chat> ParticipantInChats { get; set; }
}
public class Chat
{
    [Key, Column(Order = 0)]
    public int User1Id { get; set; }

    public virtual User User1 { get; set; }

    [Key, Column(Order = 1)]
    public int User2Id { get; set; }

    public virtual User User2 { get; set; }

    public string Channel { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Chat>()
        .HasRequired(s => s.User1)
        .WithMany(u => u.InitiatedChats)
        .HasForeignKey(s => s.User1Id)
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Chat>()
        .HasRequired(s => s.User2)
        .WithMany(u => u.ParticipantInChats)
        .HasForeignKey(s => s.User2Id)
        .WillCascadeOnDelete(false);

    base.OnModelCreating(modelBuilder);
}

The anwser comes from this post

Community
  • 1
  • 1
Alexandre Rondeau
  • 2,667
  • 24
  • 31