Entity Framework Code First ---EF Power Tool 和MySql一塊兒使用遇到的問題

關於如何使用EF Power Tool的介紹請看 http://www.cnblogs.com/LingzhiSun/archive/2011/05/24/EFPowerTool_1.html, 這裏再也不囉嗦。html

MySql裏有個默認的範例數據庫 world, 裏面有三個表,sql

image

下載Entity Framework Power Tools 安裝包, 如今已是Beta3版本,http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/數據庫

下載完安裝, 而後新建一個控制檯項目,項目名稱爲EFEntity, 而後右鍵點擊項目名,在彈出菜單中選擇Entity Framework –》Reverse Engineering Code First 。ide

image

 

選擇相應的數據庫鏈接ui

image

image

 

能夠看到引用了不應引用程序集spa

image

App.Config 也多了一個Providers子節點3d

  <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" />
    </providers>
  </entityFramework>

 

須要把Providers子節點整個刪掉,引用Mysql Connector的驅動,才能正常運行程序。code

image

另外,Power Tool 安裝的EF 默認版本是6.6.0.0, 這個版本在VS2012環境下能夠正常工做, 在VS2010 .NET 4.0 的環境下不行,要用NuGet卸載掉,而後從新安裝,以後看到的版本是4.4.0.0.server

image

 

Program.cs的代碼是:xml

   1:      class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              using (var context = new worldContext())
   6:              {
   7:                  foreach (var item in context.cities)
   8:                  {
   9:                      Console.WriteLine(item.Name + "  " + item.District + "  " + item.CountryCode);
  10:                  }
  11:                  Console.Read();
  12:              }
  13:          }
  14:      }
 
 

App.Config文件內容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="worldContext" connectionString="server=localhost;Character Set=utf8;User Id=root;password=pristine2008;Persist Security Info=True;database=world"
      providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

image

相關文章
相關標籤/搜索