我有4个表要与它们建立关系。
模型1类别
public partial class Categories
{
public Categories()
{
AssetTypes = new HashSet<AssetTypes>();
CategoryComponents = new HashSet<CategoryComponents>();
Items = new HashSet<Items>();
}
public int CategoryId { get; set; }
public DateTime? Deletedon { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
public virtual ICollection<AssetTypes> AssetTypes { get; set; }
public virtual ICollection<CategoryComponents> CategoryComponents { get; set; }
public virtual ICollection<Items> Items { get; set; }
}
这是Model 2类别组件
public partial class CategoryComponents
{
public CategoryComponents()
{
AssetComponents = new HashSet<AssetComponents>();
}
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public virtual Categories Category { get; set; }
public virtual ICollection<AssetComponents> AssetComponents { get; set; }
}
模型3“资产类型”
public partial class AssetTypes
{
public AssetTypes()
{
AssetComponents = new HashSet<AssetComponents>();
Checkins = new HashSet<Checkins>();
}
public int Id { get; set; }
public string Type { get; set; }
public string AssetName { get; set; }
public int AssetStatus { get; set; }
public string ImagePath { get; set; }
public int VendorId { get; set; }
public int CategoryId { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? DeletedOn { get; set; }
public string Description { get; set; }
public bool? ActiveStatus { get; set; }
public int SubCategoryId { get; set; }
public decimal Price { get; set; }
public virtual Categories Category { get; set; }
public virtual SubCategory SubCategory { get; set; }
public virtual Vendors Vendor { get; set; }
public virtual AssetTracks AssetTracks { get; set; }
public virtual ICollection<AssetComponents> AssetComponents { get; set; }
public virtual ICollection<Checkins> Checkins { get; set; }
}
模型4“ AssetComponents”
public partial class AssetComponents
{
public int Id { get; set; }
public string Value { get; set; }
public int AssetTypeId { get; set; }
public string Note { get; set; }
public int CategoryComponentId { get; set; }
public virtual AssetTypes AssetType { get; set; }
public virtual CategoryComponents CategoryComponent { get; set; }
}
添加迁移后出现错误
在表'AssetComponents'上引入FOREIGN KEY约束'FK_AssetComponents_CategoryComponents_CategoryComponentId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 无法创建约束或索引。查看以前的错误。
public virtual DbSet<Categories> Categories { get; set; }
public virtual DbSet<CategoryComponents> CategoryComponents { get; set; }
public virtual DbSet<AssetComponents> AssetComponents { get; set; }
public virtual DbSet<AssetTypes> AssetTypes { get; set; }
任何人都可以为我建立一个数据库。
我想接受任何身体反应。如果我想念直到我一样的东西
答案 0 :(得分:0)
您可以考虑将Restrict onDelete添加为一种行为。
modelBuilder.HasOne(x => x....).WithMany(op => op.....).IsRequired()
.HasForeignKey(@"FkId").OnDelete(DeleteBehavior.Restrict);
答案 1 :(得分:0)
对于CategoryComponents
和AssetComponents
之间的关系,这是一对多关系。
尝试将public int CategoryComponentId { get; set; }
更改为public int? CategoryComponentId { get; set; }
,以解决此错误。