Create a database and data table using the Entity Framework through code First

Source: Internet
Author: User
Tags one table connectionstrings

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.