EF连接MYSQL"/>
VS2012 + EF连接MYSQL
写博客是为了帮助其他人,少走弯路,方便自己以后查阅,如果你看了我的博客,希望你也这样做。以下内容也是我网上搜集,和我自己实践的记录,不对的地方可以留言。
工程没有采用什么框架封层,只是为了搭建这个开发环境。单工程
如果是多工程,那么在设置为启动工程的工程中也要添加下面的内容,并使所有App.config内容一致
开发环境:
VS2012
mysql 5.7.23.0
我创建的是基于4.0的winform。
首先安装基本包,工程中引用上点右键
通过NuGet将EntityFramework6
后面的安装就不能再这个图形界面上安装了,要换命令行了。我个人感觉图形安装的都是最新版本,我的开发环境造成了不能用最新版本,所以命令行下,安装以前的版本。
下图中输入命令,
MySql.Data.Entity 6.10.8 是基于。net4.5.2的,这里我不适用
换低版本
Install-Package MySql.Data.Entity -Version 6.9.12
安装完效果如下,依赖关系也会进行安装。这个开发包就全了,开始写代码 了
项目建完后,我们新建一个数据模型:右键->新建项->数据->ADO.NET实体数据模型。名字为DBContext,如下图:
如果弹出的是下图:
那么就需要安装新的工具了 Entity Framework 6 Tools for Visual Studio 2012 & 2013
地址:.aspx?id=40762
安装后是这个效果的
这里选空code first 模型
App.config内容:
<?xml version="1.0" encoding="utf-8"?>
<configuration><configSections><!-- For more information on Entity Framework configuration, visit /?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><entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="v11.0" /></parameters></defaultConnectionFactory><providers><provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /><provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers></entityFramework><system.data><DbProviderFactories><remove invariant="MySql.Data.MySqlClient" /><add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /></DbProviderFactories></system.data><connectionStrings><!--<add name="DatabaseContext" connectionString="data source=(LocalDb)\v11.0;initial catalog=DBNAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />--><add name="DataModelContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=DBNAME;user id=root;password=sql123;" providerName="MySql.Data.MySqlClient" /></connectionStrings>
</configuration>
微软的帮助文档:
下面创建类
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;namespace WindowsFormsApplication6
{public class Card{[Key]public int Id { get; set; }public string CardName { get; set; }}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;namespace WindowsFormsApplication6
{public class User{[Key]public int Id { get; set; }public string Name { get; set; }}
}
修改之前的CodeFirstContext
namespace WindowsFormsApplication2
{using System;using System.Data.Entity;using System.Linq;public class DBContext : DbContext{//您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)//使用“DBContext”连接字符串。默认情况下,此连接字符串针对您的 LocalDb 实例上的//“WindowsFormsApplication2.DBContext”数据库。// //如果您想要针对其他数据库和/或数据库提供程序,请在应用程序配置文件中修改“DBContext”//连接字符串。public DBContext(): base("name=DBContext"){}//为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First 模型//的详细信息,请参阅 /?LinkId=390109。// public virtual DbSet<MyEntity> MyEntities { get; set; }public DbSet<User> User { get; set; }public DbSet<Card> Card { get; set; }}//public class MyEntity//{// public int Id { get; set; }// public string Name { get; set; }//}
}
在入口函数中添加代码进行测试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){//创建上下文DBContext dbcontext = new DBContext();//创建数据库dbcontext.Database.CreateIfNotExists();//创建表,并将字段加入进去User u = new User();u.Name = "wang";u.Id = 1;Card c = new Card();c.CardName = "lei";c.Id = 1;//将实体赋予上下文,并添加到表里dbcontext.User.Add(u);//保存dbcontext.SaveChanges();Console.WriteLine("成功创建数据库和表");Console.ReadKey();}}
}
更多推荐
VS2012 + EF连接MYSQL
发布评论