A small problem occurs when MVC is connected to the SQL Server database.

Source: Internet
Author: User

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 ).

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.