Pooki Foo Pooki Foo - 2 months ago 7
C# Question

how to get an entity inside an other one?

I am having a small problem with a dotnet project where i need to use linq syntaxe to calll a class inside an other one .
Here's the code

table.cs

public class Table
{
public int Id { get; set; }
public bool isAvailable { get; set; }
public int Numero { get; set; }
public virtual ICollection<Reservation> IReservation { get; set; }
}


tablegroupe.cs

public class TableGroupe : Table
{
public int NbChaise { get; set; }
}


tablecouple.cs

public class TableCouple : Table
{
public bool DineeChandelle { get; set; }
}


restaurentcontext.cs

public class RestaurantContext :DbContext
{
public DbSet<Table> tTable { set; get; }
public DbSet<Menu> tMenu { set; get; }
public DbSet<Reservation> tReservation { set; get; }
public RestaurantContext() : base("RestaurentDB") {

}


tablerepository.cs

class TableRepository
{
RestaurantContext rc = null;

public TableRepository()
{
rc = new RestaurantContext();
}
public void Commit()
{
rc.SaveChanges();
}
public void AddTable(Table m)
{
rc.tTable.Add(m);
}
public TableCouple GetFirstTableCoupleAvailable(bool chandelle) {}
}


i coulndt know how to get the table wich is tablecouple

Answer

You have 2 classes that are derived from Table.

tablegroupe
tablecouple

Your table class is your baseClass I think (?). In your context you want to add the other tables instead of the base class.

public class RestaurantContext :DbContext
{
    public DbSet<TableGroupe> tTableGroupe { set; get; }
    public DbSet<TableCouple> tTableCouple { set; get; }
    public DbSet<Menu> tMenu { set; get; }
    public DbSet<Reservation> tReservation { set; get; }
    public RestaurantContext() : base("RestaurentDB") {
 }

You might want to make your Table class abstract (or virtual). Or if I misunderstsood, then you want to add Table also to your context... But then I would suggest you create an interface for base class.