mardi 13 décembre 2016

Check Entity Relationships OnDelete using EF

Consider the following scenario. I'm trying to following this post in order check all relationships entries of one specific UnitCompany entry I could have when I'm deleting it.

Affirming there are many Suppliers related with one specific UnitCompany, why HasAnyRelation(object entityObj) (see at the link) is always returning false? Even for any other entity related by one-to-many, I can't get the the current related entries.

MODEL

[Table("supplybiz.UnitCompany")]
public partial class UnitCompany
{
    public long UnitCompanyId { get; set; }
    public string UnitCompanyType { get; set; }
    public string UnitCompanyName { get; set; }

    public UnitCompany()
    {
        this.Suppliers = new HashSet<Supplier>();
    }

    public virtual ICollection<Supplier> Suppliers { get; set; }
}


[Table("supplybiz.Supplier")]
public partial class Supplier
{
    public long SupplierId { get; set; }
    public string SupplierType { get; set; }
    public string SupplierName { get; set; }

    public Supplier()
    {
        this.UnitCompanies = new HashSet<UnitCompany>();
    }

    public virtual ICollection<UnitCompany> UnitCompanies { get; set; }
}

RELATIONSHIPS

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Supplier>()
                .HasMany<UnitCompany>(uc => uc.UnitCompanies)
                .WithMany(s => s.Suppliers)
                .Map(su =>
                {
                    su.MapLeftKey("SupplierId");
                    su.MapRightKey("UnitCompanyId");
                    su.ToTable("UnitCompanySupplier");
                });
}





Aucun commentaire:

Enregistrer un commentaire