简体   繁体   中英

how to create Migration for multiple Tables with relationship.

I have 4 tables i want to make Relationship b/w these .

Model 1 Categories

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

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"

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"

 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. 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.

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

am wating any body response .if i miss samething plese till me.

you may consider to add Restrict onDelete as a behavior.

 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.

Try to change 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; } to resolve this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM