First, create an MVC project, reference EntityFramework, and create a student class
Using system;using system.collections.generic;using system.linq;using system.web;namespace EFF. models{public class Student {public int StudentID {get; set;} public string Name {get; set;} public int Age {get; set;} public string Address {get; set;} public int Sexsk {get; set;}} }
After that, create a new DBContext.cs (this should be used for creating the database)
Using system.collections.generic;using system.data.entity;using system.linq;using system.web;using EFF. models;using system.data.entity.modelconfiguration.conventions;namespace eff{Public class DBEFFContext:D Bcontext {public dbset<student> Students {get; set;}} }
Of course, we need to connect to the database
On the Web. config, add the <configuration>
<connectionStrings> <add name= "Dbeffcontext" connectionstring= "Data source=.; Initial catalog=isee;integrated security=true "providername=" System.Data.SqlClient "/> </ Connectionstrings>
Connect to database, library name: ISEE (I look at Caesar)
Then I run to see what the next Caesar ...
Things are coming out, but ....
Modify Model:student time, run again, will error, no way, had to check the
The results found:
EF Code First Migrations Database migration (is this a link?) )
Look, although solved, but every time a run requires .... In the VS Tool->nuget Package Manager, the package management console runs inside ....
Install entityframework:pm> install-package entityframework generate configuration.cs,pm> Enable-Migrations- Enableautomaticmigrations (then remember to change automaticmigrationsenabled to true) after successful execution, add the class file in the Migrations folder 201309201556388_ Initialcreate.cs,pm> Add-migration Initialcreate This should be an update. pm> update-database-verbosepm> Add-Migration Addcity executes the Package Manager console statement again pm> Update-database-verbose
The data is still there, but always feel like this, every time a change to model, you have to do a set of (always feel a bit not applicable ....) Then go and look for it.
Thanks to several references that have been greatly provided:
MVC adds fields using EF Code First Data migration
http://blog.csdn.net/kangk1992/article/details/10107363
EF Code First Migrations Database migration:
Http://www.cnblogs.com/libingql/p/3330880.html
Code first mode development in MVC, Database Generation and update (EF6):
http://blog.csdn.net/chenguang79/article/details/50847550
ASP. NET MVC EF database preliminary water