1、安装插件

在使用Visual Studio 2013添加ADO.NET实体数据模型新建连接时,默认是没有Mysql选项的。此时我们需要安装两个东西:

1、mysql-for-visualstudio:Mysql的Visual Studio插件,推荐1.2.3版本

2、mysql-connector-net:.net连接Mysql的程序,推荐6.8.3,版本。如果安装高版本可能导致一系列问题。详见:http://blog.csdn.net/niewq/article/details/41877301

2、新建ADO.NET实体数据模型

2、1、按图操作,添加实体数据模型:

2.2、一切进展貌似都很顺利。接下来你可能会看到Visual Studio给出了如下的提示:

2.3、解决方法:在NuGet的控制台输入以下命令:

Install-Package EntityFramework -Version 6.0.0
     Install-Package EntityFramework.zh-Hans -Version 6.0.0
     Install-Package MySql.Data.Entity.EF6

每个命令输入之后按回车执行,你会发现前两个都很顺利,但是第三个却报错了:

此时我们不通过NuGet添加这个引用,具体步骤为将MySQL Connector Net 6.8.3\Assemblies\v4.5(视你的项目使用的.net版本而定,我的是.net 4.5)下的所有dll文件引用进来。我的机器上安装目录如下:

全部引用

然后在应用程序配置文件中添加:<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

然后,一定要重新生成项目!!!

2.4、继续下面的步骤,成功。

3、Mysql数据库改动

接下来,就是从数据库选择表以生成EDMX文件,但是在此过程中,你可能会遇到下列问题:

VS给出了一堆的提示,但是重点就是红框内的:表“TableDetails”中列“IsPrimaryKey”的值为 DBNull。这个问题的解决方案在。我们按照文中所说,设置数据库testbak(我用的数据库):

1、重启数据库服务器。

2、use testbak;

3、set global optimizer_switch='derived_merge=OFF';

再去尝试一次,成功!!!

解决方案窗口多了很多文件:

每个实体数据模型生成一个context类,数据库每个表生成一个entity类。在Model1.edmx中包含的两个重要的文件Model1.Context.tt和Model1.tt。第一个是用于生成Context类的T4模板,第二是用于生成表映射实体类(POCO类,POCO:Plain Old CLR Object)的T4模板。

Model1.Context.cs是从System.Data.Entity.DbContext类继承。EF4.1中则是从ObjectContext类继承。DbContext类与ObjectContext类似,它对ObjcetContext类进行包装更利于开发的三种模式:CodeFirst、Model First、Database First。

4、DbContext

DbContext是EntityFramework很重要的部分,连接域模型与数据库的桥梁,是与数据库通信的主要类。

DbContext主要负责以下活动:

EntitySet:DbContext包含了所有映射到表的entities

Querying将Linq-To-Entities转译为Sql并发送到数据库

Change Tracking从数据库获取entities后保留并跟踪实体数据变化

Persisting Data根据entity状态执行Insert、update、delete命令

CachingDbContext的默认第一级缓存,在上下文中的生命周期中存储entity

Manage RelationshipDbContext在DbFirst模式中使用CSDL、MSL、SSDL管理对象关系,Code first中使用fluent api 管理关系

Object MaterializationDbContext将物理表转成entity实例对象

//DbContext实例化:
using (var ctx = newSchoolDBEntities())
{
//Can perform CRUD operation using ctx here..
} //将DbContext转为ObjectContext
using (var ctx = newSchoolDBEntities())
{
var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext;
//use objectContext here..
}

详见:http://www.cnblogs.com/xuf22/articles/5513283.html

5、增删改查操作

5.1 IDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; namespace ADO.NETEFDemo
{
public interface IDAL<T> where T : class,new()
{
/// <summary>
/// 增
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
int Add(T model); /// <summary>
/// 删
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
int Delete(Expression<Func<T, bool>> whereLambda); /// <summary>
/// 改
/// </summary>
/// <param name="whereLambda"></param>
/// <param name="propertyNames"></param>
/// <param name="perpertyValues"></param>
/// <returns></returns>
int Update(Expression<Func<T, bool>> whereLambda, string[] propertyNames, object[] perpertyValues); /// <summary>
/// 查
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
List<T> GetModelList(Expression<Func<T, bool>> whereLambda);
}
}

5.2 DAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace ADO.NETEFDemo
{
public class DAL<T> : IDAL<T> where T : class,new()
{
/// <summary>
/// 增
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int Add(T model)
{
using (testbakEntities db = new testbakEntities())
{
db.Set<T>().Add(model);
return db.SaveChanges();
}
} /// <summary>
/// 删
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public int Delete(Expression<Func<T, bool>> whereLambda)
{
using (testbakEntities db = new testbakEntities())
{
var dbQuery = db.Set<T>(); //先查询 对应表的 集合
var list = dbQuery.Where(whereLambda).ToList(); //遍历集合 里要删除的元素
foreach (var item in list)
{
//标记为 删除状态
dbQuery.Remove(item);
}
return db.SaveChanges();
}
} /// <summary>
/// 改
/// </summary>
/// <param name="whereLambda"></param>
/// <param name="propertyNames"></param>
/// <param name="perpertyValues"></param>
/// <returns></returns>
public int Update(Expression<Func<T, bool>> whereLambda, string[] propertyNames, object[] perpertyValues)
{
using (testbakEntities db = new testbakEntities())
{
//1、查询要修改的对象集合
var list = db.Set<T>().Where<T>(whereLambda).ToList(); //2、获取要修改的对象的类型
Type t = typeof(T); //3、循环要修改的实体对象,并根据要修改的属性名修改对象对应的属性值
foreach (var item in list)
{
//循环 要修改的属性 名称, 并 反射取出 t 中的 属性对象
for (int index = ; index < propertyNames.Length; index++)
{
//获取要修改的属性名
string pName = propertyNames[index]; //获取属性对象
PropertyInfo pi = t.GetProperty(pName); //调用属性对象的 SetValue方法 为当前循环的 item对象 对应的属性赋值
pi.SetValue(item, perpertyValues[index], null);
}
}
return db.SaveChanges();
}
} /// <summary>
/// 查
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public List<T> GetModelList(Expression<Func<T, bool>> whereLambda)
{
using (testbakEntities db = new testbakEntities())
{
return db.Set<T>().Where(whereLambda).ToList();
}
}
}
}

5.3 BLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; namespace ADO.NETEFDemo
{
public static class BLL<T> where T : class,new()
{
private static IDAL<T> dal = new DAL<T>(); /// <summary>
/// 新增
/// </summary>
/// <param name="model"></param>
public static int Add(T model)
{
return dal.Add(model);
} /// <summary>
/// 删除
/// </summary>
/// <param name="whereLambda"></param>
public static int Delete(Expression<Func<T, bool>> whereLambda)
{
return dal.Delete(whereLambda);
} /// <summary>
/// 修改
/// </summary>
/// <param name="whereLambda"></param>
/// <param name="propertyNames"></param>
/// <param name="perpertyValues"></param>
/// <returns></returns>
public static int Update(Expression<Func<T, bool>> whereLambda, string[] propertyNames, object[] perpertyValues)
{
return dal.Update(whereLambda, propertyNames, perpertyValues);
} /// <summary>
/// 查询
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public static List<T> GetModelList(Expression<Func<T, bool>> whereLambda)
{
return dal.GetModelList(whereLambda);
}
}
}

5.4 调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ADO.NETEFDemo
{
class Program
{
static void Main(string[] args)
{
GetArticleList(); AddArticle(); GetArticleList(); UpdateArticle(); GetArticleList(); DeleteArticel(); GetArticleList(); Console.ReadKey();
} /// <summary>
/// 更新
/// </summary>
private static void UpdateArticle()
{
int result = BLL<t_crobot_reship_articles>
.Update(e => e.title.Contains("EF"), new[] { "title", "update_time" },
new object[] { "我是使用EF修改过标题的文章", DateTime.Now });
if (result >= )
{
Console.WriteLine("更新成功");
}
else
{
Console.WriteLine("更新失败");
}
Console.WriteLine();
} /// <summary>
/// 删除
/// </summary>
private static void DeleteArticel()
{
int result = BLL<t_crobot_reship_articles>.Delete(e => e.title.Contains("EF"));
if (result >= )
{
Console.WriteLine("删除成功");
}
else
{
Console.WriteLine("删除失败");
}
Console.WriteLine();
} /// <summary>
/// 新增
/// </summary>
private static void AddArticle()
{
t_crobot_reship_articles model = new t_crobot_reship_articles();
model.create_time = DateTime.Now;
model.module_id = ;
model.adword_id = ;
model.pick_id = ;
model.vote_id = "";
model.title = "我是使用EF添加的文章";
model.content_id = ;
model.release_url = "http://www.sss.com";
model.state = true;
int result = BLL<t_crobot_reship_articles>.Add(model);
if (result >= )
{
Console.WriteLine("新增成功");
}
else
{
Console.WriteLine("新增失败");
}
Console.WriteLine();
} /// <summary>
/// 获取文章列表
/// </summary>
private static void GetArticleList()
{
List<t_crobot_reship_articles> articleList = BLL<t_crobot_reship_articles>
.GetModelList(e => e.state == true); Console.WriteLine("文章总数:" + articleList.Count.ToString());
foreach (t_crobot_reship_articles model in articleList)
{
Console.WriteLine("标题:" + model.title);
} Console.WriteLine();
}
}
}

5.5 结果

6、小结

文章中需要使用的插件下载地址:mysql-for-visualstudio-1.2.3.zipmysql-connector-net-6.8.3.zip

具体的操作步骤并不一定按文中所述的来。

使用VS2013 + EF6 + .NET4.5 连接Mysql数据库的更多相关文章

  1. 刚刚完成了在vs2013中通过 ef连接mysql数据库的工作。感觉没有想象中的简单。试了n次终于成功。故记录成功的方法,希望可以帮到大家

    分两种情况,如果你是用entity framework 5.0的时候 mysql-connector-net的版本不是很重要. MySQL For VisualStudio的版本也不重要 (这个不装就 ...

  2. 使用VS2013 + EF6 连接Mysql数据库

    使用VS2013 + EF6 + .NET4.5 连接Mysql数据库 1.安装插件 在使用Visual Studio 2013添加ADO.NET实体数据模型新建连接时,默认是没有Mysql选项的.此 ...

  3. Vs2013 使用EF6 连接mysql数据库

    最近在使用MySQL数据库,在使用EF框架连接MySQL数据库时发现了一个问题,使用DB First创建实体对象的时候会出现如下图的错误:您的项目引用了最新版实体框架….. (如下图)或者会出现新建实 ...

  4. VS2015 +EF6 连接MYSQL数据库生成实体

      VS2015 +EF6 连接MYSQL数据库生成实体   已安装软件:VS2015                       XAMPP Control Panel(Mysql服务器)      ...

  5. Entity Framework连接Mysql数据库并生成Model和DAL层

    Entity Framework (EF,ADO.NET Entity Framework)是微软官方提供的.NET平台的ORM框架.相比于LINQ TO SQL,EF框架具有很明显的优势: EF框架 ...

  6. EntityFramework 6.0< Code First > 连接 Mysql数据库(转)

    http://blog.csdn.net/kmguo/article/details/19650299 网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转载的太多 ...

  7. EntityFramework 6.0< Code First > 连接 Mysql数据库

    网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转载的太多了.找了很久,总算是配置好了,现在分享一下. 一,安装:     1.开发环境: VS2013与EF6 ...

  8. 转载:EntityFramework 6.0< Code First > 连接 Mysql数据库

    转载自:http://blog.csdn.net/kmguo/article/details/19650299 网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转 ...

  9. visualC/C++连接MySql数据库

    vs连接数据库其实就是将mysql数据库.h头文件接口.lib链接文件和dll执行文件加入到项目中.下面是配置如何加入. 转于http://www.cnblogs.com/justinzhang/ar ...

随机推荐

  1. Struts2第二篇【开发步骤、执行流程、struts.xml讲解、defalut-struts讲解】

    前言 我们现在学习的是Struts2,其实Struts1和Struts2在技术上是没有很大的关联的.Struts2其实基于Web Work框架的,只不过它的推广没有Struts1好,因此就拿着Stru ...

  2. cas-单点登录-应用说明

    单独在tomcat中启动cas 1,  我的百度网盘中有 cas  和 tomcat-cas 压缩包  http://pan.baidu.com/s/1bnxVRkF   直接解压缩就可以使用. 2, ...

  3. 微软云linux服务器FTP文件传输错误解决办法

    在微软云上新建了linux虚拟机之后,通过Xshell连接到服务器(微软云默认的账号是:azureuser,不是root),却发现通过FTP传输文件错误,一直找不到头绪,询问微软云相关人员才知道.FT ...

  4. 《HiBlogs》重写笔记[1]--从DbContext到依赖注入再到自动注入

    本篇文章主要分析DbContext的线程内唯一,然后ASP.NET Core的注入,再到实现自动注入. DbContext为什么要线程内唯一(非线程安全) 我们在使用EF的时候,可能使用相关框架封装过 ...

  5. GMF常见问题

    1.问题:连接线旁边没有文字标签和箭头 文字标签:在gmfmap里的Connection Mappping下增加Label Mapping元素:箭头:在gmfgraph里为Polyline Conne ...

  6. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  7. Linux JDK配置

    第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...

  8. GBDT(MART)概念简介

    GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree),是一种用于回归的机器学习算法,该算法由 ...

  9. CSS3D模型

    html部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  10. 解决VMware下安装Ubuntu 16.04 不支持1920X1080分辨率的问题

    解决方法: flashmx@ubuntu:~$ cvt # 192.07M9) hsync: 67.16 kHz; pclk: 173.00 MHz Modeline -hsync +vsync fl ...