Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.0 generating read only model when I create model from existing database

I am working on a custom ado.net provider and using that provider I am integrating Entity Framework support in Visual Studio 2010. I'm creating all possible mapping and reading all the related metadata from database for table objects. For my test, a table contains primary keys and and other fields. When I create a model from database using this table and I get a model with all column mappings and everything but I also get error messages that follow:

The model was generated with warnings or errors. Please see the Error List for more details. These issues must be fixed before running your application. Loading metadata from the database took 00:00:11.4799371. Generating the model took 00:00:04.2751189. Added the connection string to the App.Config file. Writing the .edmx file took 00:00:00.0005060.

If I open the .edmx file with XML editor, I see the following error:

<!--Errors Found During Generation:
  warning 6002: The table/view 'sqlfire.APP.CUSTOMERS' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
  -->

It also added a DefiningQuery with a SELECT ... statement for the table.

I am going in a circle to find a solution and desperately looking for some help on this issue.

like image 625
user2001457 Avatar asked Feb 20 '14 22:02

user2001457


People also ask

Which of the following is the correct command to create model based on your existing database in Entity Framework Core?

You use the DbContext Scaffold command to generate the model. The command has two required arguments - a connection string and a provider. The connection string will depend on your environment and database provider.

Which approach will create Entity Framework from an existing database?

Entity Framework has a well-documented approach, called reverse engineering, to create the EF Entity Classes and DbContext from an existing database.

How do you use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add.

How do I update Entity Framework model from database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.


1 Answers

From the error message looks like one of your table/view doesn't have a Primary-Key. EF needs a Primary-Key in every table in order to generate Entity keys.

You may still be able to run your application, but I strongly suggest you add primary keys as warned.

like image 97
HOKBONG Avatar answered Sep 30 '22 00:09

HOKBONG