地址:
https://dev.mysql.com/downloads/connector/net/
下载后 一直下一步安装即可
效果如下
不赘述了,直接上package文件
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.4.4" targetFramework="net48" />
<package id="Google.Protobuf" version="3.19.4" targetFramework="net48" />
<package id="K4os.Compression.LZ4" version="1.2.6" targetFramework="net48" />
<package id="K4os.Compression.LZ4.Streams" version="1.2.6" targetFramework="net48" />
<package id="K4os.Hash.xxHash" version="1.0.6" targetFramework="net48" />
<package id="MySql.Data" version="8.0.31" targetFramework="net48" />
<package id="MySql.Data.EntityFramework" version="8.0.31" targetFramework="net48" />
<package id="Portable.BouncyCastle" version="1.9.0" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net48" />
</packages>
<?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>
<connectionStrings>
<!--这里需要注意,DataSource 不要用 . ,因为“.”是sqlserver使用的-->
<add name="default" connectionString="Data Source=localhost;Initial Catalog=你的库名;user id=你的数据库账户;password=你的数据库登录密码;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<entityFramework>
<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.EntityFramework, Version=8.0.31.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider>
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
==注意连接字符串,服务地址可以用localhost,但是不能用 “.” ==
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF6ForMysql
{
public class MyDbContext : DbContext
{
public MyDbContext() : base("default")
{
}
}
}
Enable-Migrations
找到下面这个文件,修改sql生成器
代码做如下修改
namespace EF6ForMysql.Migrations
{
using MySql.Data.EntityFramework;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<EF6ForMysql.MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
//添加这行代码
SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());//设置Sql生成器为Mysql的
}
protected override void Seed(EF6ForMysql.MyDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
}
注意是添加 SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());//设置Sql生成器为Mysql的
代码
在程序包控制台输入如下代码
Add-Migration Init
效果如下
迁移数据库,输入如下命令
Update-Database
到此codefirst的配置完成
添加一个实体到dbcontext中,通过重复执行如上的 第三步和第四步,实现添加一个表到数据库
public class User
{
[Key]
public string Id { get; set; }
[MaxLength(100)]
public string Name { get; set; }
}
代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF6ForMysql
{
public class MyDbContext : DbContext
{
// 添加一个DbSet,这个就会增加一个表
public DbSet<User> Users { get; set; }
public MyDbContext() : base("default")
{
}
}
}
到此结束,关于EF的使用和一下特性,参考msdn的学习文档。