學習篇二:code first (demo)

第一步:建數(shù)據(jù)庫,建表(不建表,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節(jié)點, 這個步驟很重要。
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>

第三步:添加實體




解決辦法:
1.在數(shù)據(jù)庫執(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)系上下問類

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();

        }
    }
}

執(zhí)行:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容