A small problem occurs when MVC is connected to the SQL Server database.
As a website developer, using asp.net to develop a website is inconvenient to add function modules, so I started to learn about the mvc framework.
Today is my first day of learning the mvc framework. The first step is to connect EF to the database (SQL Server2012 database). Below I use pictures and text to describe my process.
1. Create a console application (the framework used is 4.5), and then create an object data model in the console.
2. Step 2
3. The rest is to connect to the corresponding database. I will not talk about it here.
4. The next step is to create the ADO. NET Entity database model.
5. My table Name is test1, which contains the ID and Name fields. The ID field is of the int type, and the Name field is of the nvarchar (10) type.
6. This is the code I wrote,
Class Program {static void Main (string [] args) {MVCtest2Entities dbcontext = new MVCtest2Entities (); test1 model = new test1 {ID = 2, Name = ""}; dbcontext. test1.Add (model); dbcontext. saveChanges (); Console. writeLine ("OK"); Console. readKey ();}}
MVCtest2Entities is the class name in the gray area of the image, as shown in figure
The following describes my error:
1.
Or the following error occurs:
"Unable to update EntitySet" ***** ", because it has a DefiningQuery, And the element does not support the element of the current operation.
After a morning of hard work, I finally learned why.
Cause: the primary key must be set in the database. If it is not set, the above error will occur, but the other is that the field cannot be blank (I am not sure, please forgive me for being lazy ).