Creating EF tables using the EF Designer from Database in VS 2015 and the primary keys are not generating in the resulting EF tables. RRS feed

  • Question

  • I have an SQL database with Primary keys. When I try to add an Entity Data Model using the Entity Data Model Wizard in VS 2015 and selecting EF Designer from database for what should the model contain, the resulting schema displays the tables with the primary keys. (here is a code snippet from the edmx file):

      <EntityType Name="BBS_Group_Tbl">


                <PropertyRef Name="GroupID" />


              <Property Name="GroupID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />

              <Property Name="GroupName" Type="varchar" MaxLength="255" />

              <Property Name="GroupDescription" Type="varchar" MaxLength="255" />

              <Property Name="CreatedBy" Type="char" MaxLength="20" />

              <Property Name="CreateDate" Type="datetime" />


            <EntityType Name="BBS_Msg_Tbl">


                <PropertyRef Name="MsgID" />


              <Property Name="MsgID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />

              <Property Name="TopicID" Type="int" />

              <Property Name="ReplyToID" Type="varchar" MaxLength="75" />

              <Property Name="MsgTitle" Type="varchar" MaxLength="255" />

              <Property Name="MsgText" Type="varchar" MaxLength="4000" />

              <Property Name="CreatedBy" Type="char" MaxLength="20" />

              <Property Name="CreateDate" Type="datetime" />


    The resulting ef tables (classes) that are autogenerated do not include the Key notation:


    // <auto-generated>

    //     This code was generated from a template.


    //     Manual changes to this file may cause unexpected behavior in your application.

    //     Manual changes to this file will be overwritten if the code is regenerated.

    // </auto-generated>














    publicintGroupID { get; set; }


    publicstringGroupName { get; set; }


    publicstringGroupDescription { get; set; }


    publicstringCreatedBy { get; set; }


    publicNullable<System.DateTime> CreateDate { get; set; }



    Why?   When I try to then scaffold I get errors because my tables do not have primary keys.  If I manually put the [Key] notation in the tables (and I have over 50) then whenever a table is updated from the database, those manual changes will be overwritten.   How do I get the auto generated tables to include the Primary Keys that are present in the SQL database and the EDMX schema?

    Tuesday, October 11, 2016 9:04 PM

All replies

  • Hi A2D4,

    Entity Framework relies on every entity having a key value that it uses for tracking entities. One of the conventions that code first depends on is how it implies which property is the key in each of the code first classes. That convention is to look for a property named “ID” or one that combines the class name and “ID”, such as “GroupID”. The property will map to a primary key column in the database.

    >>Why?   When I try to then scaffold I get errors because my tables do not have primary keys. 

    According to your description, it seems that your database tables have primary keys, what operation cause your tables lost primary keys.

    Best regards,

    Cole Wu

    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.

    Wednesday, October 12, 2016 1:32 AM
  • I guess I am not explaining myself well enough - let me try again.    So I have an existing database and application so I am not using Code First.

    I instead used the Entity Data Model Wizard with the option EF Designer using existing database to create my entity framework EMDX and EF Model files.   On my existing database ALL tables have primary keys.  When I use the Entity Data Model wizard I select all my tables for the EF Designer using Existing Database to use to create the EMDX schema and the EF Model files.   From my code snippets in my original question you can see that the EMDX file has the primary keys but when the Wizard creates the EF Model files it does not contain the [Key] notation.  Although I could manually put the notation in, as the EF table comments notes - if I then have to update the table from the database, any manually entered changes will be overwritten.

    If I do not manually add the [Key] notation to all 50+ generated EF Model files, when I try to scaffold my controllers using the generated EF - I get 50+ errors - one for each of the EF Model files - each one stating that the EF Model file does not contain a primary key and that I need to enter a primary key.

    So I want to know - is this a bug in the EF Designer using Existing Database Wizard or am I missing something when I use the Wizard?    

    Wednesday, October 12, 2016 1:24 PM
  • So I want to know - is this a bug in the EF Designer using Existing Database Wizard or am I missing something when I use the Wizard?    

    It seems to me that there must be a checkbox that says something like "Expose all primary and foreign" keys in the virtual model objects" when you used the wizard to create the model from the DB. And you didn't check  it on?
    Wednesday, October 12, 2016 3:36 PM
  • >If I do not manually add the [Key] notation to all 50+ generated EF Model files

    In the EF workflow you are using (traditional Database First) the C# Entity classes don't have any key metadata.  All the mapping is contained in the .edmx file.  This is by design and has always been that way.

    This does also mean that other parts of the solution that can consume those annotations won't see them.

    If it works for you consider switching to the "Code First from and Existing Database" workflow, which will generate classes with annotations and fluent configuration from your database, instead of generating an .edmx file.


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, October 12, 2016 3:41 PM
  • Wow!  It would be wonderful to start from scratch but that is not an option for me.  I don't understand why MS would create this wonderful designer that in the end does not provide what someone would need if they were needing to create from a previously built database.   I mean - what is the sense of having the option to create the EF tables this way if they do not follow the annotations necessary to make them functional???  Just because it was always that way I do not see as a valid answer - as it seems to me MS is just saying essentially that "it was always a bug and will always stay a bug"   - just my two cents from a developer who is confined to taking the traditional Database First approach and thought that MS would assist with the supposed EF Designer from existing database functionality.

    I guess I will write my own script that can update the EF files if the database is updated.

    What a shame.

    Tuesday, October 18, 2016 4:12 PM
  • No - it appears that MS is saying (see response below) that this is just how it is.   Makes no sense to me because I consider that a broken functionality.  Especially when (see below) their response is to just build it Code First - well in theory yes that would be great - in reality....not so much - I have to work with the existing database
    Tuesday, October 18, 2016 4:14 PM
  • >just build it Code First

    You don't have to start over.  There are two _different_ database-first workflows.  The one that the controller scaffolding expects is called "Code-First From and Existing Database".  It's really a database-first workflow, but it generates all the metadata like it would be in a code-first model.

    And you can always just add the attributes yourself.


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, October 18, 2016 4:22 PM