【问题标题】:Cycles and Multiple cascade Paths using EF Code First使用 EF Code First 的循环和多级联路径
【发布时间】:2013-05-21 01:26:48
【问题描述】:

我越来越糊涂了!!! 这是我第一次使用 MVC3 和 EF Code First。我收到以下错误:

在表“CityUsers”上引入 FOREIGN KEY 约束“FK_dbo.CityUsers_dbo.Cities_CityID”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束。

我已经为此工作了好几个小时,尽我所能,但无法处理!

场景是,有一个 Man 类派生出 User每个用户只属于一个城市。用户是指管理员用户。他们每个人都可以插入/更新几个城市,每个城市一次只能由一个用户修改。 我创建了第三个名为 'CityUser' 的表来跟踪修改城市记录的用户。

这是我的模型类:

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 声明关系。

现在有谁能帮我解决这个问题???!!!!!! :(

问候,

【问题讨论】:

  • 这个错误是否意味着,如果从City表中删除City,那么CityUser表中与CityID相关的记录也会被删除?如果是这样,我不知道如何处理。。我以前在编程中处理过,例如调用Delete方法时,我首先考虑记录是否有孩子。我对代码一无所知!!!请帮忙...
  • 它说有很多对CityId的引用,即Man[, User]和城市用户通过CityId引用了City,所以它要求你指定一个ON DELETE & ON UPDATE 动作,因为它无法自动推断它们。
  • 好吧..让我问你..我是否必须在父类上定义一个导航属性,例如,public virtual ICollection CityUsers {get;set;}???它有什么不同吗?我是否正确定义了外键?有什么想法吗?
  • 您应该提供导航属性以获取修改城市的用户以及用户创建的城市。希望有意义

标签: c# asp.net-mvc-3 ef-code-first


【解决方案1】:

最后,经过几个小时和大量的努力,我终于可以克服这个问题,并且永远不会忘记这个概念!!! :D 找到的解决方案:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();   

【讨论】:

    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2014-09-26
    相关资源
    最近更新 更多