關於如何使用EF Power Tool的介紹請看 http://www.cnblogs.com/LingzhiSun/archive/2011/05/24/EFPowerTool_1.html, 這里不再啰嗦。
MySql里有個默認的范例數據庫 world, 里面有三個表,
下載Entity Framework Power Tools 安裝包, 現在已經是Beta3版本,http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/
下載完安裝, 然后新建一個控制台項目,項目名稱為EFEntity, 然后右鍵點擊項目名,在彈出菜單中選擇Entity Framework –》Reverse Engineering Code First 。
選擇相應的數據庫連接
可以看到引用了不該引用程序集
App.Config 也多了一個Providers子節點
<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的驅動,才能正常運行程序。
另外,Power Tool 安裝的EF 默認版本是6.6.0.0, 這個版本在VS2012環境下可以正常工作, 在VS2010 .NET 4.0 的環境下不行,要用NuGet卸載掉,然后重新安裝,之后看到的版本是4.4.0.0.
Program.cs的代碼是:
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> |