引入外键约束可能会导致循环

时间:2018-07-25 22:06:30

标签: c# entity-framework

我正在使用ASP.NET MVC Web App更新数据库时遇到了一些问题,并且我有这些类

位置

public class Location
{

    public int ID { get; set; }
    [Required]
    [Display(Name = "Location")]
    public string location { get; set; }
}

注册课程

public class Register_User
{
    public int ID { get; set; }
    [Required]
    [Display(Name = "First Name")]
    public string first_name { get; set; }
    [Required]
    [Display(Name = "Second Name")]
    [MaxLength(25)]
    public string second_name { get; set; }
    [Required]
    [Display(Name = "Username")]
    [MaxLength(50)]
    public string username { get; set; }
    [Required]
    [MaxLength(50)]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string password { get; set; }
    [Required]
    [Display(Name = "Date of Birth")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime dob { get; set; }
    [Required]
    [Display(Name = "CNIC Number")]
    [MaxLength(50)]
    public string cnic { get; set; }
    [Required]
    [Display(Name = "Gender")]
    public Gender gender { get; set; }
    [Required]
    [Display(Name = "User Type")]
    public UserType user_type { get; set; }
    [Display(Name = "Blood Group")]
    public BloodType bloodtype { get; set; }
    public int Location { get; set; }
    [ForeignKey("Location")]
    public virtual Location user_location { get; set; }
    public string token { get; set; }
}

BloodPost

public class BloodPost
{
    public int ID { get; set; }
    [Required]
    [Display(Name = "Quantity")]
    public int quantity { get; set; }
    [Required]
    [Display(Name = "Blood Type")]
    public BloodType bloodType { get; set; }
    [Required]
    [Display(Name ="Post Date")]
    public DateTime postDate { get; set; }

    [Required]
    public int Location { get; set; }
    [ForeignKey("Location")]
    public virtual Location user_location { get; set; }

    [Display(Name = "User")]
    public int Register_User { get; set; }
    [ForeignKey("Register_User")]
    public virtual Register_User linked_user { get; set; }
}

更新数据库时,出现以下错误。

  

在表“ BloodPosts”上引入FOREIGN KEY约束“ FK_dbo.BloodPosts_dbo.Locations_Location”可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。   无法创建约束或索引。查看以前的错误。

0 个答案:

没有答案