none
Entity Framework database first says no keys defined RRS feed

  • Question

  • I am very confused.  I generated my data model using Entity Framework 5.0 from my database (database first) in which all primary keys are defined.  It brought through the edmx file which clearly shows all the keys on all the tables.  Yet, when I try to generate a controller for any of the tables I get a whole load of errors saying keys are not defined for any of my tables.

    This used to happen automatically in previous versions of Entity Framework, I swear - do I now have to go individually into each class file and define the keys in each one?  This is not only very tedious but seems unnecessary given that the EDMX file clearly knows which items are defined as keys?  Am I missing something?  Do I need a patch or something?

    Help! 

    TIA!

    Friday, July 10, 2015 7:29 PM

Answers

  • Hello msollicito,

    >> Am I missing something?  Do I need a patch or something?

    Normally, EF would help generate primary key constraint automatically, we do not need to go into each class file and define these keys and according to your description, I also made a small test by importing a table to EMD by EF5, however, all its constraints are imported to the .edmx file successfully. Here is the test environment:

    VS2013,

    .NET 4.5,

    SQL Server 2012 Express,

    EF5,

    Windows 8.1

    The test table:

    CREATE TABLE [dbo].[Order] (
    
        [OrderID]   INT          NOT NULL,
    
        [OrderName] VARCHAR (50) NULL,
    
        CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED ([OrderID] ASC)
    
    );
    

    For this case, I suggest you could:

    1.Having a try the demo I provided

    2.Using the EF6 to see if it could work for you

    If above opinions are not helpful, please sharing your detail environment with us.

    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, July 13, 2015 1:50 AM
    Moderator