Db First - skipping Foreign key if FK relation is not to a PK RRS feed

  • Question

  • Hi

    I have a DB Schema that looks something like this (I'm writing it in code.. easier to do)

    public class User


    Guid userId {get; set; } // primary key
    string Name {get; set;}
    string Location {get; set;} // not nullable, contains a Name value that is in a Location (including cascading, etc.)


    public class Location


    int LocationId {get; set;} // primary key
    string Name {get; set;} // unique (IX contraint on DB table)


    I'd like that once the mappter has run that the generated User class would contain a property

    virtual Location Location_1 {get; set;} 

    which holds a location object.

    Before you ask.. there's a reason for this kind of db structure. FKs relationships that go to a PK in another table work just fine and are picked up by the mapper, but the contruct we're dealing with here is not something we can change.



    Friday, May 15, 2015 9:05 AM


  • Hello Stephan,

    >> I'd like that once the mappter has run that the generated User class would contain a property:   virtual Location Location_1 {get; set;}

    As far as I know, in Entity Framework, this kind property would be generated only when there is a FK relationship(it is not matter 1:1 or 1:*) between the User table and the Location table, and this is a by default feature, if there are relationships in tables, Entity Framework would also generate them in the metadata, it does not support logic relationships.

    For this case, in my opinion, you could try to customize the T4 template which is used to generate these entities, however, it may not be easy to do.

    For these two specific tables, you could try to map to them to a store produce so that you could customize their CURD operations: https://msdn.microsoft.com/en-us/data/gg699321.aspx?f=255&MSPPError=-2147217396


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Monday, May 25, 2015 8:20 AM
    Monday, May 18, 2015 2:02 AM