none
Entity framework Code-First possible with 'implied/non enforced' PK and FK columns? RRS feed

  • Question

  • Hi All,

    I've got a very old database that was created before Referential Integrity (RI) was available at the database level.  This database has implied PK and FK columns defined, but the actual RI was enforced in software.

    I've tried to create a EF 6 database-first project to generate the POCOs, then I've added the necessary Fluent API key and relationship calls to define the missing keys and relationships.

    Surprisingly, I have come across all sorts of problems following relationships through navigation properties and even more problems when writing entities back to the DB.  Reading seems to work pretty well.  Small comfort.

    If I am defining all the keys and relationships in Fluent API, does EF really care if these are not physically defined in the DB?  I'd really hate to have to revert to writing my object Repositories based on ADO.Net Datasets under the covers.  :P

    Thanks for any advice.

    Corey.



    Friday, December 19, 2014 11:24 PM

Answers

  • Hello,

    >>I've got a very old database that was created before Referential Integrity (RI) was available at the database level

    Could you please tell what database you are using and which version it is? As far as I know, not all database are supported by Entity Framework, you could check the link below for a supported list:

    http://msdn.microsoft.com/en-us/data/dd363565.aspx

    >>If I am defining all the keys and relationships in Fluent API, does EF really care if these are not physically defined in the DB?

    Since it is not clear that what database you are using, I assume that it is Sql Server(or if it is other database, i would suggest you could post it to their official site forum), since you are using database first approach to generate the entity model, it would have an .edmx file which contains all the details and mapping definitions of your model. Since it should contains the metadata section in the connection string, EF won't even call OnModelCreating or any code in Fluent API in the OnModelCreating method. And as far as I know, logic relationships are not supported, Entity Framework needs a physically relationship.

    Regards.


    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.

    Monday, December 22, 2014 6:00 AM
    Moderator