[英]Entity Framework C# Introducing A Foreign Key X In Table Y May Cause Cycles OR Multiple Cascade Paths
I am developing a C# MVC application. 我正在开发C#MVC应用程序。 I am using Code First approach to model my database. 我正在使用代码优先方法对数据库进行建模。
My project had the following requirements: 我的项目具有以下要求:
Here are model classes (code first solution) to the above mentioned problem. 这是上述问题的模型类(代码优先解决方案)。
public class Company
{
public Company()
{
this.Employees = new HashSet<ApplicationUser>();
}
public int ID { get; set; }
[Required]
public string Name { get; set; }
public string Logo { get; set; }
[Display(Name="Company Description")]
public string CompanyDescription { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public virtual ICollection<ApplicationUser> Employees { get; set; }
public virtual ICollection<Client> Clients { get; set; }
public ICollection<Product> Products { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public string ProductName { get; set; }
public int CompanyID { get; set; }
public virtual Company Company { get; set; }
public virtual ICollection<AdvertisementType> AdvertisementTypes { get; set; }
}
public class AdvertisementType
{
public int AdvertisementTypeID { get; set; }
public int ProductID { get; set; }
[Display(Name = "Advertisement Name")]
public string AdvertisementTypeName { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public virtual Product Product { get; set; }
}
When I try to update the database, after creating the migrations i get the following error: 当我尝试更新数据库时,在创建迁移后出现以下错误:
Introducing FOREIGN KEY constraint 'FK_dbo.AdvertisementTypes_dbo.Products_ProductID' on table 'AdvertisementTypes' 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 or index. 无法创建约束或索引。 See previous errors. 请参阅先前的错误。
I have been trying solve this problem but unable to find any solution. 我一直在尝试解决此问题,但找不到任何解决方案。 I don't find any problem with the model classes, nor i think there are any issues with the relationship between the models. 我发现模型类没有任何问题,我也不认为模型之间的关系有任何问题。
Any suggestions or help will be useful. 任何建议或帮助将是有用的。
EDIT 编辑
Here is screenshot of Tables and their relations 这是表格及其关系的屏幕截图
in your dbContext you need to turn cascade delete to false if you want to avoid that. 在dbContext中,如果要避免这种情况,需要将层叠删除设置为false。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
You can initialize your make a List() in Product like you did with Company class... 您可以像使用Company类一样在Product中初始化make list()。
public class Product
{
public int ProductID { get; set; }
public Product()
{
this.AdvertisementTypes = new List<AdvertisementType>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.