EF代码优先自我参考

时间:2018-10-08 09:21:42

标签: entity-framework many-to-many code-first

在一种业务场景中,需要使用代码优先方法创建模型,例如 企业有很多供应商
供应商可以属于许多企业 因此需要在业务和供应商之间创建很多对很多的供应商,但是供应商在逻辑上是业务本身(自我参考)。供应商模型实际上并不存在。

如何创建与Business本身具有多对多关系并且在关联表(即BusinessVendor)中具有独立列IsActive和VendorType的BusinessVendor模型?

更新

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

}

但现在级联删除的问题为

  

介绍FOREIGN KEY约束   桌子上的'FK_dbo.BusinessVendors_dbo.Vendors_VendorId'   “ BusinessVendors”可能会导致循环或多个级联路径。指定   ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN   关键约束。无法创建约束。查看以前的错误。

0 个答案:

没有答案