Directory
Overview
Add a model
Summarize
Series Articles
[ASP. NET MVC] ASP. MVC5 Series--First project
[ASP. NET MVC] ASP. MVC5 Series--Add view
Overview
In this section we will append some classes to manage student information in the database. These classes will become the "model" part of our MVC application.
In the vs2013 version of EF (Entity Framework) EF6, we will use the EF6 for student information maintenance, by the way also learn EF6 additions and deletions.
Add a model
In Solution Explorer, right-click the Models folder and click New Item under the Add menu.
I have a student information test database, so chose to generate from the database, in order to facilitate the use of later, also lazy to create a new database.
Select New Connection
Select "Yes, include sensitive data in the connection string", if you choose the connection string above the password is replaced by a string of "*", you have to modify, so how easy to come.
Select the EF version, select Ef6 here
Select the database object, where the Student,score,course table may be used, so three data tables will be selected.
After you select the datasheet, click Finish, and a dialog box with a security warning pops up, directly determining that it is ignored.
A diagram of three tables will pop up afterwards
So far we have created three model class Student,score,course classes
The specific code is as follows:
1 //------------------------------------------------------------------------------2 //<auto-generated>3 //This code has been generated from the template. 4 //5 //manually changing this file may cause unexpected behavior in your application. 6 //If you regenerate the code, the manual changes to this file are overwritten. 7 //</auto-generated>8 //------------------------------------------------------------------------------9 Ten namespaceWolfy.FirstMVCProject.Models One { A usingSystem; - usingSystem.Collections.Generic; - the Public Partial classStudent - { - PublicStudent () - { + This. Score =NewHashset<score>(); - } + A Public intStuid {Get;Set; } at Public stringStuname {Get;Set; } - Public stringStusex {Get;Set; } - PublicSystem.DateTime Stubirthdate {Get;Set; } - PublicSystem.DateTime Stustudydate {Get;Set; } - Public stringstuaddress {Get;Set; } - Public stringStuemail {Get;Set; } in Public stringStuphone {Get;Set; } - Publicnullable<BOOL> Stuisdel {Get;Set; } to PublicNullable<system.datetime> Stuinputtime {Get;Set; } + Public intclassId {Get;Set; } - the Public VirtualCourse Course {Get;Set; } * Public VirtualIcollection<score> Score {Get;Set; } $ }Panax Notoginseng}
1 //------------------------------------------------------------------------------2 //<auto-generated>3 //This code has been generated from the template. 4 //5 //manually changing this file may cause unexpected behavior in your application. 6 //If you regenerate the code, the manual changes to this file are overwritten. 7 //</auto-generated>8 //------------------------------------------------------------------------------9 Ten namespaceWolfy.FirstMVCProject.Models One { A usingSystem; - usingSystem.Collections.Generic; - the Public Partial classCourse - { - PublicCourse () - { + This. Score =NewHashset<score>(); - This. Student =NewHashset<student>(); + } A at Public intclassId {Get;Set; } - Public stringClassName {Get;Set; } - Public stringclassdescription {Get;Set; } - - Public VirtualIcollection<score> Score {Get;Set; } - Public VirtualIcollection<student> Student {Get;Set; } in } -}
1 //------------------------------------------------------------------------------2 //<auto-generated>3 //This code has been generated from the template. 4 //5 //manually changing this file may cause unexpected behavior in your application. 6 //If you regenerate the code, the manual changes to this file are overwritten. 7 //</auto-generated>8 //------------------------------------------------------------------------------9 Ten namespaceWolfy.FirstMVCProject.Models One { A usingSystem; - usingSystem.Collections.Generic; - the Public Partial classscore - { - Public intTestID {Get;Set; } - Public intStuid {Get;Set; } + Public intclassId {Get;Set; } - Public intTestbase {Get;Set; } + Public intTestbeyond {Get;Set; } A Public intTestpro {Get;Set; } at Public stringtestname {Get;Set; } - PublicSystem.DateTime TestDate {Get;Set; } - - Public VirtualCourse Course {Get;Set; } - Public VirtualStudent Student {Get;Set; } - } in}
In the next article, we're going to create a new Schoolcontroller class that shows the data in the database and allows the user to create a student list that can add student information.
Summarize
Read a lot of similar articles on the internet is the use of Code-first way, if you write a code-first similar article, old tricks to play, boring, play new things still necessary. So since there's a new play in vs2013, why not try it?
This article is about adding a model with less content and a lot of space to say EF, because adding a model doesn't know what to say? Add a class, how to say ah? Worry about!
If your eyes are sharper, you may find that the files generated from the Add ADO entity model have *.TT files, you know! This thing can also be studied.
Reference article:
Http://www.asp.net/mvc/tutorials/mvc-5/introduction/adding-a-model