Click here to Skip to main content
15,914,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

suppose I have three tables.

1. TableA: AId, Name
2. TableB: BId, Name
3. TableC: CId, AId, BId

Table C is relational table between TableA and TableB.

I have written following code to manage entity

C#
//Class Defines
public class TableA: IdEntity //Id Property define there
{
    private ICollection<TableB> _tableBList;
public virtual string Name { get; set; }
    public virtual ICollection<TableB> TableBList
    {
        get { return _tableBList ?? (_tableBList = new List<TableB>()); }
        protected set { _tableBList = value; }
    }
}
public class TableB: IdEntity //Id Property define there
{
public virtual string Name { get; set; }
}
public class TableC: IdEntity //Id Property define there
{
public virtual int TableAId{ get; set; }
public virtual int TableBId{ get; set; }

public virtual TableA TableA{ get; set; }
public virtual TableB TableB{ get; set; }
}

// Mapping class
public class TableAMap : EntityTypeConfiguration<TableA>
{
    public TableAMap()
    {
        this.ToTable("TableA");
        this.HasKey(x => x.Id);
        this.Property(x => x.Name).IsRequired().HasMaxLength(100);
    }
}
public class TableBMap : EntityTypeConfiguration<TableB>
{
    public TableBMap()
    {
        this.ToTable("TableB");
        this.HasKey(x => x.Id);
        this.Property(x => x.Name).IsRequired().HasMaxLength(100);
    }
}
public class TableCMap: EntityTypeConfiguration<TableC>
{
    public TableCMap()
    {
        this.ToTable("TableC");

        this.HasKey(x => x.Id);

        this.HasRequired(x => x.TableB)
            .WithMany()
            .HasForeignKey(x => x.TableBId);

        this.HasRequired(x => x.TableA)
            .WithMany(x => x.TableBList)
            .HasForeignKey(x => x.TableAId);
    }
}


Now, by using object of TableC, I can access the object of TableA and List of TableB,

but I want to add one another concept there, I want to access object of TableC by using the object of TableB as following.

C#
var bObject = GetTableBById(1);
    var cObject = bObject.TableC;



Is this possible in above scenario?

Thanks
Imrankhan
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900