MVC3、如何应用EntityFramework 连接MySql 数据库
原文:MVC3、如何应用EntityFramework 连接MySql 数据库
新的一年,新的开始。
今天总结的主题是在MySql中应用EntityFramework 的Code First模式。
开发环境:Win8 + MySql5.5 +VS 2012.
第一步是在数据库中新建一个表,具体字段如下图。

在表中添加若干数据:

数据建好之后,下面就是在项目中引用EntityFramework了。
二,在项目中新建一个实体类Product
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; }
}
注意,成员名要与数据库中的名相同。
然后新建一个接口 IProductRepository
public interface IProductRepository
{
IQueryable<Product> Products { get; }
}
之后,是实现接口的类
public class EFProductRepostitory:IProductRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Entities.Product> Products
{
get { return context.Products; }
}
}
实现与数据库上下文关联
public class EFDbContext:DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Friend> Friends { get; set; }
}
然后在Controller中实现调用
public class ProductController : Controller
{
private int pageSize = ;
private IProductRepository repository; public ProductController(IProductRepository productRepository)
{
repository = productRepository;
} public ViewResult List(int page=)
{
return View(repository.Products
.OrderBy(p=>p.ProductID)
.Skip((page-)*pageSize)
.Take(pageSize));
} }
在Ninject产生Controller的类中绑定。
public class NinjectControllerFactory:DefaultControllerFactory
{
private IKernel ninjectKernel; public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
} protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
} public void AddBindings()
{
//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>();
}
最后需要注意的一点是,配置文件中写数据库连接的地方要与DbContext的类名保持一致。
<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>
MVC3、如何应用EntityFramework 连接MySql 数据库的更多相关文章
- 使用ABP EntityFramework连接MySQL数据库
ASP.NET Boilerplate(简称ABP)是在.Net平台下一个很流行的DDD框架,该框架已经为我们提供了大量的函数,非常方便与搭建企业应用. 关于这个框架的介绍我就不多说,有兴趣的可以参见 ...
- EntityFramework 6.0< Code First > 连接 Mysql数据库(转)
http://blog.csdn.net/kmguo/article/details/19650299 网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转载的太多 ...
- EntityFramework 6.0< Code First > 连接 Mysql数据库
网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转载的太多了.找了很久,总算是配置好了,现在分享一下. 一,安装: 1.开发环境: VS2013与EF6 ...
- 转载:EntityFramework 6.0< Code First > 连接 Mysql数据库
转载自:http://blog.csdn.net/kmguo/article/details/19650299 网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转 ...
- Entity Framework连接Mysql数据库并生成Model和DAL层
Entity Framework (EF,ADO.NET Entity Framework)是微软官方提供的.NET平台的ORM框架.相比于LINQ TO SQL,EF框架具有很明显的优势: EF框架 ...
- Vs2013 使用EF6 连接mysql数据库
最近在使用MySQL数据库,在使用EF框架连接MySQL数据库时发现了一个问题,使用DB First创建实体对象的时候会出现如下图的错误:您的项目引用了最新版实体框架….. (如下图)或者会出现新建实 ...
- 使用EF CodeFirst连接MySql数据库
如何使用EF CodeFirst连接MySql数据库? 我们这篇文章介绍怎么使用EF连接MySql 作者的环境 VS2017.Win10.MySql5.x 前言 一般在EF中,默认是使用SqlServ ...
- 使用VS2013 + EF6 连接Mysql数据库
使用VS2013 + EF6 + .NET4.5 连接Mysql数据库 1.安装插件 在使用Visual Studio 2013添加ADO.NET实体数据模型新建连接时,默认是没有Mysql选项的.此 ...
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
随机推荐
- c# Internet时间服务器同步
2009-02-02 17:48 8226人阅读 评论(2) 收藏 举报 服务器internetc#socketstringwindows 需要用到的名空间 using System.Net; usi ...
- cocos2d-x Mask的实现及优化
转自:http://blog.ch-wind.com/cocos2d-x%E4%B8%ADmask%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8F%8A%E4%BC%98%E5%8C ...
- 【M26】限制某个class所能产生的对象数量
1.每当产生一个对象,必定调用构造方法.因此,禁止产生对象的做法就是,将所有的构造方法声明为private. 2.只有在类的内部才可以访问private成员,有两层含义:在类的内部可以访问this的p ...
- bat 简单命令实现编译cocos2d-x android项目
新建一个compile_cmd.bat文件,存放需要执行的命令: cocos compile -p android -j 4 然后,如果直接运行这个文件,在编译完之后命令行窗口会自动退出,这样我们无法 ...
- delphi 获取 TreeView选中的文件路径
//获取 TreeView选中的文件路径 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, G ...
- android 动态改变屏幕方向
LANDSCAPE与PORTRAIT 范例说明 要如何通过程序控制Activity的显示方向?在Android中,若要通过程序改变屏幕显示的方向,必须要覆盖 setRequestedOrientati ...
- HTTP协议报文、工作原理及Java中的HTTP通信技术详解
一.web及网络基础 1.HTTP的历史 1.1.HTTP的概念: HTTP(Hyper Text Transfer Protocol ...
- Helpers\TableBuilder
Helpers\TableBuilder Table builder helper is a class that would help you to create tables in MySQL ( ...
- 记录asp.net网站停止运行原因的代码
记录网站是什么原因导致停止运行还是有必要的,下面是具体的实现方式. protected void Application_End(object sender, EventArgs e) { Recor ...
- Autowired properities class
1. Properties类 @ConfigurationProperties(locations = "classpath:build.properties") @JsonInc ...