Development environment WIN10 Entity Framework6.0 MVC5.0 development tool VS2015 SqlServer2012
1. Create context contexts to inherit dbcontext, and create additional business classes that will create the corresponding data tables.
1 Public classAccountcontext:dbcontext2 {3 PublicAccountcontext ():Base("Accountcontext") { }4 PublicDbset<sysuser> SysUsers {Get;Set; }5 PublicDbset<sysrole> Sysroles {Get;Set; }6 PublicDbset<sysuserrole> Sysuserroles {Get;Set; }7 8 protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder)9 {TenModelbuilder.conventions.remove<pluralizingtablenameconvention>(); One } A}
2. Join the context node in the EntityFramework node of the Web. config configuration file
1<entityFramework>2<contexts>3<context type="Mvcdemo.dal.accountcontext,mvcdemo"disabledatabaseinitialization="false">4<databaseinitializer type="Mvcdemo.dal.accountinitializer,mvcdemo"></databaseInitializer>5</context>6</contexts>7<defaultconnectionfactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">8<parameters>9<parameter value="v11.0"/>Ten</parameters> One</defaultConnectionFactory> A<providers> -<provider invariantname="System.Data.SqlClient"Type="System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver"/> -</providers> the</entityFramework>
3. Configure the database connection string, note here: name= "Accountcontext", the value of the database connection name is consistent with the previous context-dependent database connection name.
1 <connectionStrings>2 <add name="accountcontext" connectionstring="Data source=.; Database=mvcdemo;uid=sa;pwd=sunfast; " providername="System.Data.SqlClient"/>3 </ Connectionstrings>
4. Run the project, the database will be created automatically, the database file is placed by default in SQL Server installation directory, the database will create more than one table [dbo]. [__migrationhistory] records the log information for creating the database.
Create a database and data table using the Entity Framework through code First