我遇到了一个奇怪的问题,我已经知道为什么会发生这种异常,以及如何通过在其中一个属性中将DisClaing WillCascadeOnDelete解析为False来解决它。
但我的问题似乎是另一种, 这是我的代码:
Memeber:
public class Memeber
{
public int MemberID { get; set; }
public virtual ICollection<Message> SentMessages { get; set; }
public virtual ICollection<Message> RecievedMessages { get; set; }
}
public class Message
{
public long MessageID { get; set; }
public int SenderMemberId{ get; set; }
public virtual ICollection<Member> RecipientsMembers { get; set; }
[ForeignKey("SenderMemberId")]
public virtual Member SenderMember { get; set; }
}
这是映射: 在消息配置中:
this.HasRequired(c => c.SenderMember)
.WithMany(c => c.SentMessages)
.HasForeignKey(c => c.SenderMemberId)
.WillCascadeOnDelete(false);
并在MemberConfiguration:
this.HasMany(c => c.RecievedMessages)
.WithMany(c => c.RecipientsMembers)
.Map(cm =>
{
cm.ToTable("MessageJoinMemeber");
cm.MapLeftKey("MessageId");
cm.MapRightKey("MemberId");
});
当你看到一条消息时,一个发件人使用ForeignKey SenderID,一对多的bidirrectional。 和会员有2个消息列表,一个作为接收者,一个列表作为发送者。 我尝试在其中一个关系中禁用级联删除(第一个) 但我仍然从SQL引擎得到同样的问题。 此外,我试图从消息部分而不是从成员部分定义关系 用:
this.HasMany(c => c.RecipientsMembers)
.WithMany(c=> c.RecievedMessages)
.Map ( cm =>
{
cm.ToTable("MessageJoinMemeber");
cm.MapLeftKey("MessageId");
cm.MapRightKey("MemberId");
});
但是Allways我得到了这个错误:
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)
答案 0 :(得分:3)
这应该可以正常工作(与你的相同)
public class Member
{
public int MemberID { get; set; }
public string Name { get; set; }
public virtual ICollection<Message> SentMessages { get; set; }
public virtual ICollection<Message> RecievedMessages { get; set; }
}
public class Message
{
public long MessageID { get; set; }
public int SenderMemberId { get; set; }
public virtual ICollection<Member> RecipientsMembers { get; set; }
[ForeignKey("SenderMemberId")]
public virtual Member SenderMember { get; set; }
}
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Message>()
.HasRequired(c => c.SenderMember)
.WithMany(c => c.SentMessages)
.HasForeignKey(c => c.SenderMemberId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Member>()
.HasMany(c => c.RecievedMessages)
.WithMany(c => c.RecipientsMembers)
.Map(cm =>
{
cm.ToTable("MessageJoinMemeber");
cm.MapLeftKey("MessageId");
cm.MapRightKey("MemberId");
});
...
var member = new Member { Name = "sender1" };
db.Messages.Add(new Message
{
SenderMember = new Member { Name = "sender1" },
RecipientsMembers = new List<Member>
{
new Member { Name = "receiver1" },
new Member { Name = "receiver2" },
new Member { Name = "receiver3" },
}
});
db.SaveChanges();
...但如果您仍有问题,可以手动制作连接表......
// public virtual ICollection<Message> RecievedMessages { get; set; }
public virtual ICollection<MessageJoinMember> Recieved { get; set; }
...
// public virtual ICollection<Member> RecipientsMembers { get; set; }
public virtual ICollection<MessageJoinMember> Recipients { get; set; }
...
public class MessageJoinMember
{
public long MessageID { get; set; }
public Message Message { get; set; }
public int MemberID { get; set; }
public Member Member { get; set; }
}
...
modelBuilder.Entity<Message>()
.HasRequired(c => c.SenderMember)
.WithMany(c => c.SentMessages)
.HasForeignKey(c => c.SenderMemberId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<MessageJoinMember>()
.HasKey(x => new { x.MessageID, x.MemberID });
modelBuilder.Entity<MessageJoinMember>()
.HasRequired(x => x.Message)
.WithMany(x => x.Recipients)
.HasForeignKey(x => x.MessageID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<MessageJoinMember>()
.HasRequired(x => x.Member)
.WithMany(x => x.Recieved)
.HasForeignKey(x => x.MemberID)
.WillCascadeOnDelete(false);
// remove the implicit many-to-many here
...
var message = new Message { SenderMember = new Member { Name = "sender1" }, };
db.MessageJoinMembers.Add(new MessageJoinMember { Message = message, Member = new Member { Name = "receiver1" } });
db.MessageJoinMembers.Add(new MessageJoinMember { Message = message, Member = new Member { Name = "receiver2" } });
db.MessageJoinMembers.Add(new MessageJoinMember { Message = message, Member = new Member { Name = "receiver3" } });
db.Messages.Add(message);
db.SaveChanges();