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 ();
}