In code one, after writing and running, our model synchronizes to db, but often we need to write the model to build the database, rather than wait until the UI part is finished and run before we see the database, with the help of the NuGet package Manager Allows us to complete the code=>db immediately.
Preparation: 1. I develop tools for VS2012, using the. NET Framework 4.5 (EF5);
2. Then ensure that the latest "NuGet Package Manager" has been installed in the VS extension;
3. The commonly used three line nuget is used for the EF command (which can, of course, be obtained directly with the command "Get-help entityframework"): "Enable-migrations"-enabled in the corresponding project, "Add-migration < name > "--Build scaffolding," update-database "--Submit (Apply code based migration)
4. New Class Library project "Codefirst".
Begin:
1. In VS Open the Package management console, enter the "Get-help NuGet" return, you can view some of the commands.
2. In the package management console input "Install-package entityframework", this process must ensure that the network, such as not networked, I have a way to create a new entity Framework in the project (such as "ADO." NET Entity Data Model ") item, and then delete the item. Before executing the above command, make sure the following illustration shows
At this point you can see that the project has more than one file "Packages.config", as well as a reference to the "entityframework" of the assembly. In the solution directory, the folder "packages" (physical directory, not the solution directory), if we want to copy the project, it should be together with the directory copy, otherwise to the other directory before compiling the project still need to execute "install-package EntityFramework, this is where you need to be aware of adding solutions to source management.
3. Add the project folder "Models" under the project and write the following three classes and an enumeration in the folder (EF5 support for enumerations is great):
<summary>
///sex
///</summary> public
enum Gender
{
Female = 0,
Male,
Ladyboy
}
<summary>
///person name
///</summary> public
class PersonName
{public
string FirstName {get; set;}
public string LastName {get; set;}
public string FullName
{get {a return
string. Format (' {0} {1} ', FirstName, LastName);}}
Public abstract class Basemodel
{public
Basemodel ()
{this
. Createdtime = DateTime.Now;
This. Lastupdatedtime = DateTime.Now;
}
Public DateTime createdtime {get; set;}
public string creater {get; set;}
Public DateTime lastupdatedtime {get; set;}
public string Lastupdater {get; set;}
}
[Table ("UserInfo")]
public class Usermodel:basemodel
{
[Key]
[databasegenerated (Databasegeneratedoption.none)]
public string LoginName {get; set;}
[MaxLength (m)]
public string Password {get; set;}
public int Age {get; set;}
Public Gender Gender {get; set;}
Public DateTime? lastlogintime {get; set;}
Public PersonName Name {get; set;}
}