EF+MYSQL+DDD frame Construction (i)--code first preparation

Source: Internet
Author: User
Tags mysql in connectionstrings

Prepare MySQL for vs tool

Mysql-connector-net-6.8.3.msi Network Address: http://pan.baidu.com/s/1jHDWINk

Mysql-for-visualstudio-1.1.4.msi Network Address: Http://pan.baidu.com/s/1eSwemno

Open vs Tools-"library Package Manager-" Package Manager control Platform

Installing EF and MySQL dependency packages

1.pm> install-package mysql.data.entity-version 6.9.8

2.pm> Install-package EntityFramework

Configure the database connection string after the installation is complete connectionstrings

<connectionStrings>    <add name="mycontext" connectionstring= " Data source=localhost;port=3307;initial catalog=mybook;user id=root;password=root; " providername="MySql.Data.MySqlClient "/>  </connectionstrings >

Create a users entity and a Myusercontext context object under the Model folder in a Web project

Users entity class

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingsystem.web;namespacePMS. web.models{/// <summary>    ///Users entity Objects/// </summary>     Public classUsers { Public intId {Get;Set; }  Public stringUserName {Get;Set; } //The default string is mapped to MySQL in the Longtext type, and after the length of the addition it becomes varchar.[MaxLength ( -)]         Public stringPassWord {Get;Set; } }}

Myusercontext Context Object

usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingsystem.web;namespacePMS. web.models{[Dbconfigurationtype (typeof(MySql.Data.Entity.MySqlEFConfiguration))] Public classMyusercontext:dbcontext { PublicMyusercontext ():Base("Name=mycontext")//connectionstring name in Web. config        {        }         PublicDbset<users> Users {Get;Set; } }}

HomeController.cs

usingPMS. Web.models;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacePMS. web.controllers{ Public classHomecontroller:controller { PublicActionResult Index () {initdata (); returnView (); }        Private voidInitData () {Database.setinitializer (NewDropcreatedatabasealways<myusercontext>()); varContext =NewMyusercontext (); //insert a row of valuesContext. Users.add (NewUsers {UserName ="Ef6-mysql-code-first" }); Context.        SaveChanges (); }    }}

Run the project if "Model compatibility cannot be checked because the database does not contain model metadata." You can only check model compatibility for databases created with code first or code first migration. ”

Migrating it in Visual Studio

1.PM> Enable-migrations

2. Configuring the Automaticmigrationsenabled property of a configuration

namespacePMS. web.migrations{usingSystem; usingSystem.Data.Entity; usingSystem.Data.Entity.Migrations; usingSystem.Linq; Internal Sealed classConfiguration:dbmigrationsconfiguration<pms. Web.models.myusercontext>    {         PublicConfiguration () {automaticmigrationsenabled=true; }        protected Override voidSeed (PMS. Web.Models.MyUserContext context) {//This method is called after migrating to the latest version. //You can use the dbset<t>. AddOrUpdate () Helper extension method//To avoid creating duplicate seed data.            e.g. //            //context. People.addorupdate (//p = p.fullname,//new Person {FullName = "Andrew Peters"},//new Person {FullName = "Brice Lambson"},//new Person {FullName = "Rowan Miller"}//    ); //        }    }}

3.PM> Update-Database - Force

Re-run the project. This is already working

EF+MYSQL+DDD frame Construction (i)--code first preparation

Related Article

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.