第一步:建數(shù)據(jù)庫(kù),建表(不建表,app.config里連接寫好也可)
第二步:新建項(xiàng)目
在 - 工具 - 庫(kù)程序包管理器 - 程序包管理器控制臺(tái) 這里 默認(rèn)項(xiàng)目, 在PM>后 輸入
Install-Package EntityFramework -Version 6.0.0
Install-Package EntityFramework.zh-Hans -Version 6.0.0
Install-Package MySql.Data.Entity.EF6(這一步我沒(méi)install進(jìn)去)故在此處添加引用。
這時(shí)候在providers 里 添加一個(gè)mysql.data.MysqlClint節(jié)點(diǎn), 這個(gè)步驟很重要。
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
第三步:添加實(shí)體
解決辦法:
1.在數(shù)據(jù)庫(kù)執(zhí)行:
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>
第五步:新建表類,和聯(lián)系上下問(wèn)類
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; }
}
}
第六步:測(cè)試
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();
}
}
}
執(zhí)行: