MVC3, how to apply entityframework connection MySQL Database

Source: Internet
Author: User
Tags connectionstrings

Original: MVC3, how to apply entityframework connection MySQL Database

A new year, a new beginning.

Today's summary of the topic is the application of the EntityFramework code first mode in MySQL.

Development environment: Win8 + MySql5.5 +vs 2012.

The first step is to create a new table in the database with the exact word Geru.

Add some data to the table:

After the data has been built, the following is a reference to entityframework in the project.

Second, create a new entity class product in the project

public class Product
{
public int ProductID {get; set;}
public string Name {get; set;}
public string Description {get; set;}
Public decimal price {get; set;}
public string Category {get; set;}
}

Note that the member name is the same as the name in the database.

Then create a new interface iproductrepository

public interface Iproductrepository
{
Iqueryable<product> products {get;}
}

After that, it is the class that implements the interface

public class Efproductrepostitory:iproductrepository

{

Private Efdbcontext context = new Efdbcontext ();

Public iqueryable<entities.product> Products

{

get {return context. Products; }

}

}

Implementation associated with a database context

public class Efdbcontext:dbcontext
{
Public dbset<product> Products {get; set;}
Public dbset<friend> Friends {get; set;}
}

Then implement the call in the controller

 Public classProductcontroller:controller {Private intPageSize =4; Privateiproductrepository repository;  PublicProductcontroller (iproductrepository productrepository) {repository=productrepository; }         PublicViewResult List (intPage=1)        {            returnView (repository. Products. (P=P.productid). Skip (Page-1)*pageSize).        Take (pageSize)); }    }

Binds in the class that ninject the controller.

  Public classNinjectcontrollerfactory:defaultcontrollerfactory {PrivateIkernel Ninjectkernel;  Publicninjectcontrollerfactory () {Ninjectkernel=NewStandardkernel ();        Addbindings (); }        protected OverrideIController getcontrollerinstance (System.Web.Routing.RequestContext requestcontext, Type controllertype) { returnControllertype = =NULL?NULL: (IController) Ninjectkernel.get (Controllertype); }         Public voidaddbindings () {//mock<iproductrepository> Mock = new mock<iproductrepository> (); //Mock. Setup (M = m.products). Returns (new list<product> {//New product{Name= "Football", price=25},//New product{Name= "basketball", price=30},//new product{name= "Pingpang", price=40}//}.            AsQueryable ()); //ninjectkernel.bind<iproductrepository> (). Toconstant (mock.            Object); Ninjectkernel.bind<iproductrepository> (). To<efproductrepostitory>(); Ninjectkernel.bind<IFriend> (). To<effriend>(); }

Finally, it is important to note that the location of the write database connection in the configuration file is consistent with the DbContext class name.

<connectionStrings> <!--<add name="defaultconnection"Providername="System.Data.SqlClient"connectionstring="Data source= (LocalDb) \v11.0;initial catalog=aspnet-sportstore.ui-20121214161900;integrated Security=SSPI; attachdbfilename=| Datadirectory|\aspnet-sportstore.ui-20121214161900.mdf"/>--> <add name="Efdbcontext"connectionstring="Database=sportstore;data Source=localhost; User Id=root; Password=root"Providername="MySql.Data.MySqlClient"/> </connectionStrings>

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.