循环和多级联路径使用EF代码优先
本文关键字:EF 代码 多级 级联 路径 循环 | 更新日期: 2023-09-27 18:15:14
我很困惑!!这是我第一次使用MVC3和EF Code first。我得到以下错误:
引入外键约束FK_dbo.CityUsers_dbo。表'CityUsers'上的Cities_CityID'可能导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他外键约束。无法创建约束。
我已经工作了几个小时,尽我所能,但就是无法处理它!
这个场景是,有一个Man类,从这个类派生出User。每个用户只属于一个城市。这里的用户是指管理员用户。每个用户都可以插入/更新多个城市,并且每个城市一次只能由一个用户修改。我已经创建了第三个名为'CityUser'的表来跟踪修改City记录的用户。这是我的模型类:
public class Man
{
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public long ID { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(20)]
[LocalizedAttribute("FName")]
public string FName { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(20)]
[LocalizedAttribute("LastName")]
public string LastName { get; set; }
//------------------------------------------------------------//
[Required]
[RegularExpression("^[0-9]+$", ErrorMessageResourceName = "ErrorOnlyNumbers", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources))]
[LocalizedAttribute("Mobile")]
public string Mobile { get; set; }
//------------------------------------------------------------//
[LocalizedAttribute("Phone")]
[RegularExpression("^[0-9]+$", ErrorMessageResourceName = "ErrorOnlyNumbers", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources))]
public string HomePhone { get; set; }
//------------------------------------------------------------//
[RegularExpression("^[0-9]+$")]
[LocalizedAttribute("IDCardNumber")]
public string IDCardNumber { get; set; }
//------------------------------------------------------------//
[RegularExpression("^[0-9]+$")]
[LocalizedAttribute("NationalCode")]
public string NationalCode { get; set; }
//------------------------------------------------------------//
[MaxLength(10)]
[LocalizedAttribute("DOB")]
public int DOB { get; set; }
//------------------------------------------------------------//
[Required]
public int CityID { get; set; }
[ForeignKey("CityID")]
public virtual City CityParent { get; set; }
//------------------------------------------------------------//
[MaxLength(100)]
[LocalizedAttribute("Address")]
public string Address { get; set; }
//------------------------------------------------------------//
[LocalizedAttribute("PostalCode")]
public string PostalCode { get; set; }
//------------------------------------------------------------//
[MaxLength(255)]
[LocalizedAttribute("PhotoPath")]
public string PhotoPath { get; set; }
}
public class User : Man
{
[MaxLength(20)]
[LocalizedAttribute("Username")]
public string UserName { get; set; }
//------------------------------------------------------------//
[DataType(DataType.Password)]
[MaxLength(100), MinLength(6, ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorPasswordLength")]
[LocalizedAttribute("Password")]
public string Password { get; set; }
//------------------------------------------------------------//
[DataType(DataType.Password)]
[Compare("Password", ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorConfirmPassword")]
[LocalizedAttribute("ConfirmPassword")]
public string ConfirmPassword { get; set; }
//------------------------------------------------------------//
[DataType(DataType.EmailAddress, ErrorMessageResourceType = typeof(MAHAL_E_MA_Model.Properties.Resources), ErrorMessageResourceName = "ErrorEmailInvalid")]
[MaxLength(20)]
[RegularExpression(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+'.[a-zA-Z]{2,}")]
[LocalizedAttribute("Email")]
public string Email { get; set; }
//------------------------------------------------------------//
[MaxLength(30)]
[LocalizedAttribute("Title")]
public string Title { get; set; }
//------------------------------------------------------------//
[MaxLength(10)]
[LocalizedAttribute("HireDate")]
public int HireDate { get; set; }
//------------------------------------------------------------//
[LocalizedAttribute("ReportsTo")]
public long ReportsTo { get; set; }
[ForeignKey("ReportsTo")]
public virtual IList<User> ReportsChild { get; set; }
}
public class City
{
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int CityID { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(20)]
[LocalizedAttribute("Name")]
public string Name { get; set; }
//------------------------------------------------------------//
[LocalizedAttribute("PhoneCode")]
public int PhoneCode { get; set; }
//------------------------------------------------------------//
[Required, MaxLength(10)]
public int ModifiedDate { get; set; }
}
public class CityUser
{
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int CityUserID { get; set; }
//------------------------------------------------------------//
[Required]
public long ModifiedByUserID { get; set; }
[ForeignKey("ModifiedByUserID")]
public virtual User OperatorUser { get; set; }
//------------------------------------------------------------//
[Required]
public int CityID { get; set; }
[ForeignKey("CityID")]
public virtual City City { get; set; }
}
但是当EF5开始创建数据库时,前面提到的错误出现了!!我能做些什么呢?我读了很多博客,对数据模型做了一些修改。但仍然无法摆脱这个错误....顺便说一下,我想使用DataAnnotations来声明关系。
现在有没有人能帮我摆脱这个问题??!!!!(
认为,
最后,我可以经过几个小时和大量的努力,通过这种方式,我永远不会忘记这个概念!!: D找到的解决方案:
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();