实体框架5.0代码首先与MySQL在WPF中 [英] Entity framework 5.0 code-first with MySQL in WPF
问题描述
此演练对SQL Express非常有用:
http://msdn.microsoft.com/en-us/library/gg197522(v = VS.103).aspx
This walkthrough works great with SQL Express: http://msdn.microsoft.com/en-us/library/gg197522(v=VS.103).aspx
我会喜欢它与MySQL一起工作。我做了一些研究,但我发现的技术都没有能够为我做。理想情况下,我想做这样的事情:
I would like it to work with MySQL. I've done some research but none of the techniques I've found has been able to do it for me. Ideally I would like to do something like this:
<entityFramework>
<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
</entityFramework>
这不起作用(已安装MySQL Connector Net 6.5.4和MySql.Data )。我尝试从IDbConnection工厂派生,如下所示:
http://www.vworker.com/RentACoder/misc/BidRequests/ShowBidRequest.asp?lngBidRequestId=1563829
This doesn't work (I have MySQL Connector Net 6.5.4 installed & MySql.Data referenced). I've tried deriving from IDbConnection factory as shown in this class here: http://www.vworker.com/RentACoder/misc/BidRequests/ShowBidRequest.asp?lngBidRequestId=1563829
然后使用:
<entityFramework>
<defaultConnectionFactory type="SchoolModel.MySqlConnectionFactory, SchoolModel" />
但不行无论是。有没有人可以给我一些关于如何使这个工作的指针?
but that doesn't work either. Can anybody please give me some pointers as to how to get this to work?
非常感谢。
推荐答案
要在VS2012上使用连接器6.5.4与代码优先EF5,您需要:
To use Connector 6.5.4 with code-first EF5 on VS2012 you need:
- 安装MySql连接器6.5.4 msi
-
打开VS2012 x86命令提示符作为管理员并执行:
- Install MySql Connector 6.5.4 msi
Open VS2012 x86 Command Prompt as Admin and execute:
gacutil / iC: \\ Program Files(x86)\MySQL\Connector NET 6.5.4 \Assemblies\v4.0\mysql.data.dll
gacutil / iC:\Program Files(x86)\\ \\ MySQL\Connector NET 6.5.4 \Assemblies\v4.0\mysql.data.entity.dll
gacutil /i "C:\Program Files (x86)\MySQL\Connector NET 6.5.4\Assemblies\v4.0\mysql.data.dll" gacutil /i "C:\Program Files (x86)\MySQL\Connector NET 6.5.4\Assemblies\v4.0\mysql.data.entity.dll"
添加到项目的应用程序.config这个代码到< configuration>
section:
Add in your project's App.config this code to <configuration>
section:
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add
name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,
Version=6.5.4.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"
/>
</DbProviderFactories>
</system.data>
现在,将MySql.Data和MySql.Data.Entity的引用添加到您的解决方案中,这样的代码(我创建MySqlConnection,然后传递给MyDbContext的构造函数)
Now add references to MySql.Data and MySql.Data.Entity to your solution and some code like this (I create MySqlConnection, then pass it to constructor of MyDbContext)
public class MyDbContext : DbContext
{
public MyDbContext(DbConnection connection) : base(connection, true) { }
public DbSet<Product> Products { get; set; }
}
[Table("sund_jshopping_products")]
public class Product
{
[Key]
[Column("product_id")]
public int Id { get; set; }
[Column("product_ean")]
public string Ean { get; set; }
[Column("product_manufacturer_id")]
public int OperatorId { get; set; }
[Column("months_status")]
public string MonthsStatus { get; set; }
[Column("extra_field_5")]
public string SideId { get; set; }
}
这篇关于实体框架5.0代码首先与MySQL在WPF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!