0

I'm trying create database using EF Code First method.

I have 3 tables with relationships.

+--------+                 +---------+
|  Users |                 | Groups  |
+--------+                 +---------+
|        |  ----------->   |         |
|________|                 |_________|

    |                            |
    |      +--------------+      |
    +--->  |Group Messages|  <---+
           +--------------+
           |              |
           |______________|

Arrows means cascade delete direction.

public DBUser()
        {
            GroupMessages = new Collection<DBGroupMessage>();
            Groups = new Collection<DBGroup>();
        }    
        public Guid Id { get; set; }
        public virtual ICollection<DBGroupMessage> GroupMessages { get; set; }
        public virtual ICollection<DBGroup> Groups { get; set; }

public class DBGroup
    {
        public DBGroup()
        {
            GroupMessages = new Collection<DBGroupMessage>();
        }
        public Guid Id { get; set; }                   
        [Required]
        public Guid OwnerID { get; set; }    
        public DBUser Owner { get; set; }    
        public virtual ICollection<DBGroupMessage> GroupMessages { get; set; } 
    }

public class DBGroupMessage
    {
        public DBGroup Group { get; set; }    
        [Required]
        public Guid GroupID { get; set; }    
        public Guid Id { get; set; }    
        public DBUser Owner { get; set; }    
        [Required]
        public Guid OwnerID { get; set; }
    }

PK and FK defined with Fluent API.

 modelBuilder.Entity<DBGroup>().HasKey(t => t.Id);
 modelBuilder.Entity<DBGroup>().HasRequired(t => t.Owner).WithMany(t => t.Groups);

modelBuilder.Entity<DBGroupMessage>().HasKey(t => t.Id);
modelBuilder.Entity<DBGroupMessage>().HasRequired(t => t.Owner).WithMany(t => t.GroupMessages);
modelBuilder.Entity<DBGroupMessage>().HasRequired(t => t.Group).WithMany(t => t.GroupMessages);
modelBuilder.Entity<DBUser>().HasKey(t => t.Id);
modelBuilder.Entity<DBUser>().HasMany(t=>t.GroupMessages).WithRequired(t=>t.Owner).WillCascadeOnDelete(false);

While creating model it throws System.Data.SqlClient.SqlException in EntityFramework.dll.

Message:

Introducing FOREIGN KEY constraint 'FK_dbo.DBGroupMessages_dbo.DBUsers_OwnerID' on table 'DBGroupMessages' may cause cycles or multiple cascade paths.

Google didn't help me -_-

Rajesh Dhiman
  • 1,888
  • 1
  • 17
  • 30
SUDALV
  • 91
  • 9
  • I understand problem, but don't understand way to solve this. Cascade update disabled in table DBUser for GroupMessages (last code string in question). – SUDALV Feb 13 '14 at 09:19

2 Answers2

0

It's throwing an exception on a different table than you're calling WillCascadeOnDelete(false) on. The FK constraint is on the DBGroupMessages table. I'm not terribly familiar with the syntax of Fluent API, but you should find some good examples here on Stack Overflow.

For example this one.

Community
  • 1
  • 1
Andrew Flanagan
  • 4,267
  • 3
  • 25
  • 37
  • Yes, it was helpful. I was added `.HasForeignKey()` to all One-To-Many relationships in Fluent API and call `.WillCascadeOnDelete(false)` after `.HasForeignKey()`in those relationships, that have cycles or multiple cascade paths. – SUDALV Feb 13 '14 at 11:07
0

There is a circular recursion happening in your entities design. Possible solution for this would be changing the DBGroupMessage class as shown below

public class DBGroupMessage
{
    public DBGroup Group { get; set; }    
    [Required]
    public Guid GroupID { get; set; }    
    public Guid Id { get; set; }    
    public DBUser Owner { get; set; }    
    public Guid? OwnerID { get; set; }
}

What I am suggesting is make owner as optional. I hope this should solve your issue.

Naga Sreenivas
  • 312
  • 3
  • 12