第一步:建数据库,建表(不建表,app.config里连接写好也可)

第二步:新建项目

在 - 工具 - 库程序包管理器 - 程序包管理器控制台 这里 默认项目, 在PM>后 输入
Install-Package EntityFramework -Version 6.0.0
Install-Package EntityFramework.zh-Hans -Version 6.0.0
Install-Package MySql.Data.Entity.EF6(这一步我没install进去)故在此处添加引用。
这时候在providers 里 添加一个mysql.data.MysqlClint节点, 这个步骤很重要。
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
第三步:添加实体



解决办法:
1.在数据库执行:
set global optimizer_switch='derived_merge=off';
set optimizer_switch='derived_merge=off';
select @@optimizer_switch;
select @@GLOBAL.optimizer_switch;
use <<database name>>;
set global optimizer_switch='derived_merge=OFF';

第四步:删除多余文件,修改app.config

app.config
<?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.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.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"></provider>
</providers>
</entityFramework>
<connectionStrings>
<add name="testEntities" connectionString="Data Source=localhost;port=3306;Initial Catalog=testcodefirstEntities;user id=root;password=0301;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
</configuration>
第五步:新建表类,和联系上下问类
testtable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestOne
{
public class testtable
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Mycontext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestOne
{
public class MyContext : DbContext
{
public MyContext()
: base("name=testEntities")
{
}
public DbSet<testtable> testtables { get; set; }
}
}
第六步:测试
program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestOne
{
class Program
{
static void Main(string[] args)
{
var context = new MyContext();
var test = context.testtables.Where(x => x.Id > 0).ToList();
//context.testtables.Add(new testtable { Name = "55" });
//context.SaveChanges();
int count = test.Count();
Console.Write(count);
Console.ReadLine();
}
}
}
执行:
