c# - 使用 EF Code First 的循环和多级联路径

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

我很迷茫!!! 这是我第一次使用 MVC3 和 EF Code First。我收到以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.CityUsers_dbo.Cities_CityID' on table 'CityUsers' 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.

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

场景是,有一个 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 声明关系。

现在有没有人可以让我摆脱这个问题???!!!! :(

问候,

最佳答案

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

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();   

关于c# - 使用 EF Code First 的循环和多级联路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752000/

相关文章:

c# - 静态类和单例类性能有什么区别(内存管理)

asp.net-mvc-3 - ASP.NET MVC3 - 文本框渲染的数据注释和最大长度/大小

entity-framework - Entity Framework 代码优先的性能

asp.net-mvc-3 - 如何配置 DbContext 以使用 Oracle ODP.Net 和 EF Code First?

c# - 如何在 Entity Framework 中设置两个一对多关系?

c# - 手动复位事件,自动复位事件

c# - 带有关联的 Signin-oidc 页面直接访问错误 - 如何重定向?

c# - 什么是NullReferenceException,如何解决?

asp.net-mvc - ASP.NET MVC 3 最佳实践/设计

c# - ASP.NET MVC 3 使用升序、降序选项自定义排序 WebGrid