1

In one of business scenario need to create Model in code first approach like Business has many Vendors
Vendors can belongs to many Businesses So need to create many to many between Business and Vendors, but Vendor is logically Business itself (self reference). Vendor Model is not exist physically.

How to create BusinessVendor model with many to many relationship with Business itself and separate columns IsActive and VendorType in association table i.e. BusinessVendor?

Update:

    public class Business
    {
        public Business()
        {
            this.Users = new HashSet<User>();
            this.Departments = new HashSet<Department>();
            this.Addresses = new HashSet<Address>();            
            this.BusinessProducts = new HashSet<BusinessProduct>();
            this.Vendors = new HashSet<Vendor>();
        }

        [Key]
        public int BusinessId { get; set; }

        [Required]
        [Display(Name = "Business Name")]
        public string Name { get; set; }

        [Required]
        [Display(Name = "PAN Number")]
        public string PAN { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect Email Format")]
        public string Email { get; set; }

        public string Fax { get; set; }

        [Required]
        [Display(Name = "Registration Number")]
        public string RegistrationNumber { get; set; }

        [Required]
        [Display(Name ="Business ID")]
        public string DisplayName { get; set; }     

        public virtual ICollection<Address> Addresses { get; set; }
        public virtual ICollection<User> Users { get; set; }
        public virtual ICollection<Department> Departments { get; set; }        

        public virtual ICollection<BusinessProduct> BusinessProducts { get; set; }

        public virtual ICollection<CategoryOfProduct> ProductCategories { get; set; }

        public virtual ICollection<Vendor> Vendors { get; set; }

    }

public class Vendor
    {

        [Key]
        public int VendorId { get; set; }

        [Required, ForeignKey("Client")]
        public int BusinessId { get; set; }     

        public int VendorTypeId { get; set; }

        [ForeignKey("VendorTypeId")]
        public virtual VendorType VendorType { get; set; }

        public bool IsActive { get; set; }

        public virtual Business Client { get; set; }

        public virtual ICollection<Business> Businesses { get; set; }


    }


protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
modelBuilder.Entity<Business>()
                  .HasMany(c => c.Vendors)
                  .WithMany(c => c.Businesses)                
                  .Map(op =>
                  {
                      op.MapLeftKey("BusinessId");
                      op.MapRightKey("VendorId");
                      op.ToTable("BusinessVendors");                      
                  });

}

But now getting issue with Cascade delete as

Introducing FOREIGN KEY constraint 'FK_dbo.BusinessVendors_dbo.Vendors_VendorId' on table 'BusinessVendors' 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. See previous errors.

Nitin
  • 29
  • 5
  • I think it may be the solution of your problem: [Self Referencing Many-to-Many relations](https://stackoverflow.com/questions/39728016/self-referencing-many-to-many-relations) – Abdul Mueed Shahid Oct 08 '18 at 09:54
  • @AbdulMueed i believe its one to many example and not applicable to my scenario. – Nitin Oct 08 '18 at 11:44

0 Answers0