Use Entity Framework to create a Model

Source: Internet
Author: User

In ASP. net mvc, you can use Entity Framework to create a Model.

In the created Model. the edmx file, essentially an XML file, mainly stores the object design diagram of the Model; the other is. designer. cs files, database objects encapsulated by classes.

In the. Designer. cs file, there is an Enitities class inherited from ObjectContext, which is used to represent the entire Model class. It also creates a public attribute named after the actual table name for each table in the database. You can directly operate on the table. When operating the database, you must first create an Entities object, and then you can perform AddTo, DeleteObject, and SaveChanges, or call the public attribute mentioned above to modify the table.

In the. Designer. cs file, there are also objects created for each database table. The names of these objects are the same as those of the table. It can be used to store data.

Take Model name as sseModel and table name as News as an example. During the Add operation, the Code is as follows:

 

C # code
Private sseEntities _ db;
Public NewsController ()
{
_ Db = new sseEntities ();
}
Public void AddNews (FormCollection form)
{
News news = News. CreateNews (1, DateTime. Now, "Zidane", "123 ",
"Zidane", "Fuck", true, true );
_ Db. AddToNews (news );
_ Db. SaveChanges ();
}

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.