[英]Getting “Foreign Key constraint” even though cascade delete is turned off
I have the following user model: 我有以下用户模型:
public class User
{
public Guid Id { get; set; }
[ForeignKey("Location")]
public Guid LocationId { get; set; }
public Location Location { get; set; }
[ForeignKey("MainPhoneNumber")]
public Guid? MainPhoneNumberId { get; set; }
[ForeignKey("Mailbox")]
public Guid? MailboxId { get; set; }
[ForeignKey("Fax")]
public Guid? FaxId { get; set; }
[MaxLength(50)]
public string UserName { get; set; }
[ForeignKey("Conference")]
public Guid? ConferenceId { get; set; }
public virtual PhoneNumber MainPhoneNumber { get; set; }
public virtual PhoneNumber Mailbox { get; set; }
public virtual PhoneNumber Fax { get; set; }
public virtual PhoneNumber Conference { get; set; }
public virtual ICollection<AddOn> AddOns { get; set; }
}
I also added the following rules using Fluent Api: 我还使用Fluent Api添加了以下规则:
modelBuilder.Entity<User>()
.HasRequired(usr=>usr.Location)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasOptional(usr => usr.Fax)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasOptional(usr => usr.Conference)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasOptional(usr => usr.Mailbox)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasOptional(usr => usr.MainPhoneNumber)
.WithMany()
.WillCascadeOnDelete(false);
I would expect that these fluent rules will help me with any Cascade delete - problems. 我希望这些流利的规则可以帮助我解决所有Cascade删除问题。 But still I receive the following error when trying to run "
Update-Database
" inside the console: 但是尝试在控制台中运行“
Update-Database
”时仍然收到以下错误:
Introducing FOREIGN KEY constraint 'FK_dbo.User_dbo.PhoneNumber_FaxId' on table 'User' may cause cycles or multiple cascade paths.
在表'User'上引入FOREIGN KEY约束'FK_dbo.User_dbo.PhoneNumber_FaxId'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 Could not create constraint.
无法创建约束。 See previous errors.
请参阅先前的错误。
Note: Please be aware that this is NOT a duplicate from that question : Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? 注意:请注意, 这不是该问题的重复项 : 引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么?
As you can see: The suggestions made there in the most topvoted answer are already included in my code (Fluent part) 如您所见:在最高投票答案中提出的建议已包含在我的代码中(流利的部分)
(Update) THis is the PhoneNumber Class: (更新)这是PhoneNumber类:
public class PhoneNumber
{
public Guid Id { get; set; }
[ForeignKey("PhoneNumberPool")]
public Guid PhoneNumberPoolId { get; set; }
public virtual PhoneNumberPool PhoneNumberPool { get; set; }
public int Length { get; set; }
}
PhoneNumberPool itself has a few simple properties (string, int, etc.) and a reference to "Location" (as well as the User). PhoneNumberPool本身具有一些简单属性(字符串,整数等)和对“位置”(以及用户)的引用。
will this datamodel work for you? 这个数据模型对您有用吗?
this way you can add new types of phone numbers 这样您可以添加新的电话号码类型
public enum PhoneNumberType {Main, Mailbox, Fax, Conference, Other}
PhoneNumber{
public PhoneNumberType NumberType{get;set;};
public string Number{get;set;};
//other properties
public virtual User{get;set;}
}
public class User
{
public Guid Id { get; set; }
[ForeignKey("Location")]
public Guid LocationId { get; set; }
public Location Location { get; set; }
[MaxLength(50)]
public string UserName { get; set; }
public virtual List<PhoneNumber> PhoneNumbers { get; set; }
public virtual ICollection<AddOn> AddOns { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.