Question

I have a table with a simple foreign key relationship, with 2 fields both acting as foreign keys to the same table. It looks like this:

create table Car
(
   CarID int primary key not null identity(1,1),

   FrontSeatID int,
   BackSeatID  int,

   foreign key(FrontSeatID) references Seat(SeatID),
   foreign key(BackSeatID) references Seat(SeatID)
)

create table Seat
(
   SeatID int primary key not null identity(1,1),
   SeatName varchar(50)
)

In entity framework, I retreive an object of type 'Car'. Besides having the integer values of 'BackSeatID' and 'FrontSeatID', tt has 2 Entity objects- one is called 'Seat' and the other is called 'Seat1'.

How do I know which Entity 'Seat' and 'Seat1' point to without checking the ID values? I know obviously I could check the ID's and see that they point to BackSeat and FrontSeat respectively, but is this ordering consistent? How could I check this mapping in the code myself, and why doesn't EntityFramework give a clear distinction on which object this entity points to?

I've done a 'find' in the entire solution for the 'Seat1' and cannot find it anywhere...

Was it helpful?

Solution

If you open the ef designer and right click on the entity you want to check, then from the context menu select Table Mapping (at least I think that is what it is called as I do not have a pc in front of me right now) It will show you which table the entity maps to and which columns each property maps to (along with the data types etc)

OTHER TIPS

Navigation properties are named purely by the entity that they point to (as you've seen). It doesn't take the naming of the column in the foreign key table into account, as it would be unrealistic to try to tackle all (or even most) of how someone might name that column.

How, exactly, are you trying to tell which is which? Are you asking if you can determine it programatically or are you using the designer and want to be able to tell which column is supplying the value for the navigation property?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top