简体   繁体   English

实体框架5.0代码优先于WPF中的MySQL

[英]Entity framework 5.0 code-first with MySQL in WPF

This walkthrough works great with SQL Express: http://msdn.microsoft.com/en-us/library/gg197522(v=VS.103).aspx 本演练适用于SQL Express: http//msdn.microsoft.com/en-us/library/gg197522(v = VS.103).aspx

I would like it to work with MySQL. 我希望它能与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>

This doesn't work (I have MySQL Connector Net 6.5.4 installed & MySql.Data referenced). 这不起作用(我安装了MySQL Connector Net 6.5.4并引用了MySql.Data)。 I've tried deriving from IDbConnection factory as shown in this class here: http://www.vworker.com/RentACoder/misc/BidRequests/ShowBidRequest.asp?lngBidRequestId=1563829 我已尝试从IDbConnection工厂派生,如此课程所示: http ://www.vworker.com/RentACoder/misc/BidRequests/ShowBidRequest.asp?lngBidRequestId=1563829

and then using: 然后使用:

      <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? 任何人都可以给我一些关于如何使其工作的指示?

Many thanks. 非常感谢。

To use Connector 6.5.4 with code-first EF5 on VS2012 you need: 要在VS2012上使用带有代码优先EF5的Connector 6.5.4,您需要:

  1. Install MySql Connector 6.5.4 msi 安装MySql Connector 6.5.4 msi
  2. Open VS2012 x86 Command Prompt as Admin and execute: 以管理员身份打开VS2012 x86命令提示符并执行:

    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" 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 \\组件\\ V4.0 \\ mysql.data.entity.dll”

  3. Add in your project's App.config this code to <configuration> section: 在项目的App.config中添加此代码到<configuration>部分:

     <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> 
  4. 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) 现在将MySql.Data和MySql.Data.Entity的引用添加到您的解决方案和一些代码(我创建MySqlConnection,然后将其传递给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; } } 

Connector 6.5.4 does not support code-first with EF 5. Actually it does not support code first. Connector 6.5.4不支持EF 5的代码优先。实际上它不支持代码。

You can try using dot net connector (at least the trial version). 您可以尝试使用dot net connector(至少是试用版)。

Did you set the Datasource to MySQL while setting up your db connection. 在设置数据库连接时是否将数据源设置为MySQL Also, click and make sure "Test Connection" succeeds before trying a connection directly from the code. 此外,单击并确保“测试连接”成功,然后直接从代码尝试连接。

在此输入图像描述

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM