1 Install-package entityframework
2 Creating an Entity class
public class blog{Public int BlogId {get; set;} public string Name {get; set;}}
3 Creating a context
public class Eftestcontext:dbcontext
{
Public Eftestcontext (): Base ("Name=sqlmonitor") {}
Public dbset<cgroup> CGroups {get; set;}
Public dbset<address> Addresses {get; set;}
Public dbset<contact> Contacts {get; set;}
protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
Base. Onmodelcreating (ModelBuilder);
Modelbuilder.entity<contact> (). Hasoptional (c = c.address)
. Withoptionaldependent (add = Add.) Contact);
Modelbuilder.entity<cgroup> (). Hasmany (c = c.contacts)
. Withrequired (c = c.cgroup). Willcascadeondelete (FALSE);
Modelbuilder.entity<easyuiefwebapp.dal.efmodels.contact> (). Hasoptional (c = c.cgroup)
// . Withmany (c = c.contacts). Willcascadeondelete (TRUE);
}
}
4 Set the Data Link link note to match the name of the context, otherwise create the local database
<connectionStrings> <add name= "SQLMonitor" connectionstring= "Data source= (LocalDb) \v11.0;initial catalog=sqlmonitor;integrated security=sspi; "providername=" System.Data.SqlClient "/> </ Connectionstrings>
Enable-migrations
Execute the above command in NuGet, if the error, 1 please see whether to write errors, 2 is the EF project, 3 after compiling try 4 to see if the version of NuGet is too old
6 Update-database
Update to the database. Note that some of the article is written add-migrations initialcreate I executed the error, no solution,
With Update-database, there is no error, and the database is updated correctly.
7 add-migration Note the following names, such as add-migration modifyusertable
Similar version-controlled stuff
EntityFramework start a small trial