title: EF5 And cycles or multiple cascade paths, FOREIGN KEY
body: <p>I got a wierd problem, i already know about why normaly this exception happen and how to solve it by Disablaing WillCascadeOnDelete to False in one of the properties.</p>
  
  <p>But my problem seems to be other kind,
  Here is my code :</p>
  
  <p>Memeber :</p>
  
  <pre><code> public class Memeber
         {
        public int MemberID { get; set; }
        public virtual ICollection&lt;Message&gt; SentMessages { get; set; }
        public virtual ICollection&lt;Message&gt; RecievedMessages { get; set; }
         }
  
      public class Message
      {
        public long MessageID { get; set; }
        public int SenderMemberId{ get; set; }
        public virtual ICollection&lt;Member&gt; RecipientsMembers { get; set; }
        [ForeignKey("SenderMemberId")]
        public virtual Member SenderMember { get; set; }
      }
  </code></pre>
  
  <p>And here is the mapping :
   in Message configuration:</p>
  
  <pre><code>    this.HasRequired(c =&gt; c.SenderMember)
              .WithMany(c =&gt; c.SentMessages)
              .HasForeignKey(c =&gt; c.SenderMemberId)
              .WillCascadeOnDelete(false);
  </code></pre>
  
  <p>and in MemberConfiguration :</p>
  
  <pre><code> this.HasMany(c =&gt; c.RecievedMessages)
          .WithMany(c =&gt; c.RecipientsMembers)
  
          .Map(cm =&gt;
          {
              cm.ToTable("MessageJoinMemeber");
              cm.MapLeftKey("MessageId");
              cm.MapRightKey("MemberId");
  
          });
  </code></pre>
  
  <p>As you see a message  hase a sender with ForeignKey SenderID, one to many bidirrectional.
  and Member has 2 list of messages one as reciever and one list as sender.
  I tried to disableing cascading on delete in one of those relation(first one)
  but i still get the same problem from SQL engine.
  Also i tried to  define the relation from Message part and not from member part
   with :</p>
  
  <pre><code>        this.HasMany(c =&gt; c.RecipientsMembers)
                 .WithMany(c=&gt; c.RecievedMessages)
                 .Map ( cm =&gt;
                  {
                  cm.ToTable("MessageJoinMemeber");
                  cm.MapLeftKey("MessageId");
                  cm.MapRightKey("MemberId");
  
                  });
  </code></pre>
  
  <p>But Allways i get this Error :</p>
  
  <pre><code> System.Data.SqlClient.SqlException was unhandled by user code
    HResult=-2146232060
    Message=Introducing FOREIGN KEY constraint 'FK_dbo.MessageJoinMemeber_dbo.Messages_MemberId' on table 'MessageJoinMemeber' 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.
    Source=.Net SqlClient Data Provider
    ErrorCode=-2146232060
    Class=16
    LineNumber=1
    Number=1785
    Procedure=""
    Server=KINGPC
    State=0
    StackTrace:
         at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
         at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
         at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
  </code></pre>