[英]Code First : Unable to create 1 to many relationship. Creates 0…1 to many instead
[英]how to create Migration for multiple Tables with relationship.
我有 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; }
}
这是模型 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“资产组件”
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; }
任何人都可以为我做一个数据库。Plzzzz
我正在等待任何身体反应。如果我错过了同样的事情,请告诉我。
您可以考虑添加 Restrict onDelete 作为行为。
modelBuilder.HasOne(x => x....).WithMany(op => op.....).IsRequired()
.HasForeignKey(@"FkId").OnDelete(DeleteBehavior.Restrict);
对于CategoryComponents
和AssetComponents
之间的关系,是一对多的关系。
尝试更改public int CategoryComponentId { get; set; }
public int CategoryComponentId { get; set; }
public int CategoryComponentId { get; set; }
到public int? CategoryComponentId { get; set; }
public int? CategoryComponentId { get; set; }
public int? CategoryComponentId { get; set; }
解决这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.