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>