1, create a database, the name of their own, MDF files and log file location of their own definition, do not need to create a table, followed by the Codefirst automatically created, in fact, the database can also create their own
2, create asp.net MVC empty Project
3, create a model, Similar to the following
using System.ComponentModel.DataAnnotations; using
System.ComponentModel.DataAnnotations.Schema; namespace Cpager.models {[Table ("MP3", Schema = "dbo")]//table name and owner public class MP3 {[Key,databasegener Ated (databasegeneratedoption.identity)]//primary KEY and self-increasing public int Id {get; set;}
Music Fragment number 1,2,3,4 ... [MaxLength ()]//nvarchar Maximum length 30, if not added, the default is max public string Singer_name {get; set;} Singer Name [MaxLength (4)] public string Singer_sex {get; set;} Singer Sex [MaxLength (5)] public string Singer_type {get; set;} Singer category [MaxLength] public string Song_name {get; set;} Song name [MaxLength (5), Column ("song_id", TypeName = "char")]//data type mapping to CHAR public string song_id {get;} Music fragment ID, in 5-bit lowercase letters [MaxLength] [MaxLength] public string Lyrics {get; set;}//Music fragment lyrics Pu Blic int? times {get; set;}//Music Fragment Time}}
4, add the EF reference, Codefirst is based on the EF
5, Add context class, similar to the following, note that reference
Using System.Data.Entity;
Namespace Cpager.models
{public
class Nicemusicdbcontext:dbcontext
{public
dbset<mp3> MP3 { Get Set }
}
}
6, the configuration file to add a connection string, similar to the following, note the location
</configSections>
<connectionStrings>
<add name= "Nicemusicdbcontext" connectionstring= " Data source=cxc;initial catalog=nicemusic;integrated security=true "providername=" System.Data.SqlClient "/>"
</connectionStrings>
<appSettings>
7, create the class to populate the data, similar to the following
Using System.Collections.Generic;
Using System.Data.Entity;
Namespace Cpager.models
{public
class Createdata:dropcreatedatabaseifmodelchanges<nicemusicdbcontext >
{
protected override void Seed (Nicemusicdbcontext context)
{
new list<mp3> {new
MP3 { Singer_name= "Deng Zi", singer_sex= "female", singer_type= "Chinese female singer", song_name= "bubble", song_id= "AAAAA", lyrics= "...", time=11},
new MP3 {singer_name= "Xinzhe", singer_sex= "male", singer_type= "Chinese male Singer", Song_name= "cat", song_id= "Aaaab", lyrics= " Shiwan a fat man afraid of the crash Bo Vb Ali Dharma ", time=12},
}. ForEach (A=>context. MP3. Add (a));
Context. SaveChanges ();}}
8, add data initialization code in Global.asax, similar to the following
Using System.Web;
Using SYSTEM.WEB.MVC;
Using System.Web.Routing;
Namespace Cpager
{public
class mvcapplication:httpapplication
{
protected void Application_Start ()
{
System.Data.Entity.Database.SetInitializer (new Models.createdata ());
Arearegistration.registerallareas ();
Routeconfig.registerroutes (routetable.routes);}}
9, this is the end of most of the work, but has not added a controller, now add a home controller and index corresponding to the view
10, this time to run, you will find that there is no data table, not to fill in the data
11, the last step is very important, Adding code to the controller is similar to the following
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using Cpager.models;
Namespace Cpager.controllers
{public
class Homecontroller:controller
{
Nicemusicdbcontext M = new Nicemusicdbcontext ();
Get:home public
ActionResult Index ()
{
var m = M.mp3. ToList ();
return View ();}}
That is, you must call the data context contexts once to generate the table and populate
the data. Run it and see the effect, OK.