问题
I have a project in MVC4 (VS 2012) with Entity Framework. I have created a model by connecting to SQL Server 2008. Works great.
Now I want to add one or more tables to the model.
Here's what I did:
- opened the .edmx file.
- Right clicked and selected Update model from the database.
- Under the Add tab, it shows Tables, Views and Stored Procedure.
But none of them is selectable. Am I missing something here?
回答1:
Murali, if I understood well, the problem is that you didn't make changes to the database since you've created the model. If this statement is correct, take a look at this:
Add tables starting from the model is a Model-first approach. Starting from code is a Code-first approach. However, you are trying to update the model from a existing database. That is a Database-first approach. For a database-first approach, the steps you mentioned are right.
However, in this approach you shouldn't be trying to develop new tabes starting from the model. Instead, you should open your sql server management studio (or database project) and code the new schema there. Then, after coding the updates in the schema, you go back to your model and follow the steps that you described.
Hope it helps.
回答2:
make sure that your table have at least one primary key in order to get added to the entity model. It works accurately when your table (which is to be added) have one primary key.
来源:https://stackoverflow.com/questions/17786140/add-new-table-to-ef