简体   繁体   English

如何为具有关系的多个表创建迁移。

[英]how to create Migration for multiple Tables with relationship.

I have 4 tables i want to make Relationship b/w these .我有 4 张表,我想与这些表建立关系。

Model 1 Categories模型 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; }
}

Here is Model 2 CategoryComponents这是模型 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; }
}

Model 3 "AssetTypes"模型 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; }
    }

Model 4 " AssetComponents"模型 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; }
    }

when i add Migration then Get error当我添加迁移然后获取错误

Introducing FOREIGN KEY constraint 'FK_AssetComponents_CategoryComponents_CategoryComponentId' on table 'AssetComponents' may cause cycles or multiple cascade paths.在表 'AssetComponents' 上引入 FOREIGN KEY 约束 'FK_AssetComponents_CategoryComponents_CategoryComponentId' 可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 Could not create constraint or index.无法创建约束或索引。 See previous errors.请参阅以前的错误。

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; }

could any body make a DB for me .Plzzzz任何人都可以为我做一个数据库。Plzzzz

am wating any body response .if i miss samething plese till me.我正在等待任何身体反应。如果我错过了同样的事情,请告诉我。

you may consider to add Restrict onDelete as a behavior.您可以考虑添加 Restrict onDelete 作为行为。

 modelBuilder.HasOne(x => x....).WithMany(op => op.....).IsRequired()
            .HasForeignKey(@"FkId").OnDelete(DeleteBehavior.Restrict);

For relationship between CategoryComponents and AssetComponents , it is one-to-many relationship.对于CategoryComponentsAssetComponents之间的关系,是一对多的关系。

Try to change public int CategoryComponentId { get; set; }尝试更改public int CategoryComponentId { get; set; } public int CategoryComponentId { get; set; } public int CategoryComponentId { get; set; } to 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; } to resolve this error. public int? CategoryComponentId { get; set; }解决这个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码优先:无法创建一对多关系。 创建0…1到很多 - Code First : Unable to create 1 to many relationship. Creates 0…1 to many instead 从两个表中获取信息以进行编辑时出错 - 一对一关系。 C# - Error getting information from two tables for editing - one-to-one relationship. C# 无法确定etaxiDataModel关系的主要结尾。 多个添加的实体可以具有相同的主键 - Unable to determine the principal end of the etaxiDataModel relationship. Multiple added entities may have the same primary key 无法确定 X 关系的主端。 多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the X relationship. Multiple added entities may have the same primary key 映射“一对多”关系的正确方法。 在多个实体中具有相同关系时 - Correct way of mapping a 'one to many' relationship. When having the same relation in multiple entities 外键在 ASP.NET 中是 NULL 的一对多关系。 如何将其添加到控制器中? - Foreign key is NULL in ASP.NET in 1 to many relationship. How do I add it in controller? 如何基于数据/ id关系从SQL查询中提取数据。 使用ASP.net WebMatrix - How to pull data from a SQL Query based on data/id relationship. Using ASP.net WebMatrix 无法确定“ MyApp.Data.AAA_BBB”关系的主要结尾。 多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the 'MyApp.Data.AAA_BBB' relationship. Multiple added entities may have the same primary key 无法确定“ Vehicle_VehicleClass”关系的主要结尾。 多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the 'Vehicle_VehicleClass' relationship. Multiple added entities may have the same primary key 如何在ASP.NET实体框架中的2个表之间创建关系 - How to create relationship between 2 tables in ASP.NET Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM