Is it possible to have an inverse navigation property in entity framework? -


i have these 2 entities, have added marked property gives me error (the error being: unable determine principal end of association between types ...)

public class lot {    [key]    public int id { get; set; }     public int vehicle_id { get; set; }     [foreignkey("vehicle_id")]    public virtual vehicle vehicle { get; set; } } 

and

public class vehicle {    public int id { get; set; }    public virtual lot lot { get; set; } // * property added } 

a vehicle may or may not belong in lot, lot have 1 vehicle.

i needed inverse navigation property between these 2 entities vehicle class such if there lot associated vehicle can access lot vehicle.

is possible , if how do without error?

no, entity framework not support this. entity framework requires tables in zero-to-one/one-to-one relationship share primary key. you've got separate lot id , vehicle ids, best can map as

public class vehicle {   public int id { get; set; }   public virtual icollection<lot> lots { get; set; } } 

and access vehicle.lots.singleordefault(). lets ef treat one-to-many relationship, you know "many" never more one.


Comments