在实体框架 4 和代码优先开发 CTP 中使用 MySql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3283820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using MySql with Entity Framework 4 and the Code-First Development CTP
提问by Dusda
I thought I'd experiment a bit with Scott Guthrie's latest poston code-first dev with Entity Framework 4. Instead of using Sql Server, I'm trying to use MySql. Here are the relevant parts of my web.config (this is an Asp.Net MVC 2 app):
我想我会尝试使用 Scott Guthrie 的最新文章,关于使用实体框架 4 的代码优先开发。我尝试使用 MySql,而不是使用 Sql Server。这是我的 web.config 的相关部分(这是一个 Asp.Net MVC 2 应用程序):
<connectionStrings>
<add name="NerdDinners"
connectionString="Server=localhost; Database=NerdDinners; Uid=root; Pwd=;"
providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<system.data>
<DbProviderFactories>
<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.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
Just like the tutorial, I'm expecting EF4 to generate the db for me automatically. Instead, it throws a ProviderIncompatibleException, with an inner exception complaining that the NerdDinners database doesn't exist.
就像教程一样,我希望 EF4 自动为我生成数据库。相反,它抛出一个 ProviderIncompatibleException,内部异常抱怨 NerdDinners 数据库不存在。
Fair enough; I went and created the MySql db for it just to see if things would work, and got another ProviderIncompatibleException instead. This time, "DatabaseExists is not supported by the provider".
很公平; 我去为它创建了 MySql 数据库只是为了看看事情是否可行,并得到了另一个 ProviderIncompatibleException 。这次,“提供程序不支持DatabaseExists”。
I'll admit, this is the first time I'm really delving into Entity Framework (I've stuck mostly to Linq to Sql), and this is all running on the Code-First CTP released only last week. That said, is there something I'm doing wrong here, or a known problem that can be worked around?
我承认,这是我第一次真正深入研究实体框架(我主要坚持使用 Linq to Sql),而且这一切都在上周发布的 Code-First CTP 上运行。也就是说,我在这里做错了什么,或者一个可以解决的已知问题?
采纳答案by Darkside
Right, finally got it working with a few points of interest.
是的,终于让它与一些兴趣点一起工作。
- Cannot create a DB, must exist already
- You have to create a connection string for each DB contest using the DBContext name (in the above example a connectionstring must exist with the name "NerdDinners"), not just a default one (else it will use SQL)
- It will use the name of the DBSet name you use to define your context as the name of the table, so be careful when naming them.
- 无法创建数据库,必须已经存在
- 您必须使用 DBContext 名称为每个数据库竞赛创建一个连接字符串(在上面的示例中,连接字符串必须存在名称为“NerdDinners”),而不仅仅是默认的(否则它将使用 SQL)
- 它将使用您用来定义上下文的 DBSet 名称作为表的名称,因此命名它们时要小心。
All in all, a long road but there in the end
总而言之,路漫漫其修远兮
**Update Another point to note, when deploying your MVC site using MySQL you will most like need also add a DataFactory to your web.config. Usually because of the difference in MySql connectors out there and the versions of MySQL that are supported. (answer found through other sources after much head scratching) Just add:
**更新 另一点要注意,当使用 MySQL 部署您的 MVC 站点时,您最喜欢还需要将 DataFactory 添加到您的 web.config。通常是因为 MySql 连接器和支持的 MySQL 版本不同。(经过多次摸索后通过其他来源找到的答案)只需添加:
<system.data>
<DbProviderFactories>
<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.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
As a seperate section to your web.config making sure to set the version numberof the MySQL.Data.dll you deploy with the site (also a good idea to "copy as local" your MySQL DLLs to ensure compatibility.
作为 web.config 的单独部分,确保设置与站点一起部署的 MySQL.Data.dll的版本号(将 MySQL DLL“复制为本地”以确保兼容性也是一个好主意。
回答by ianhorton
Another point of interest - If you add the "MySQL Data Provider" entry to your local machine.config (C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config in my case) you can then connect to your MySql instance through Visual Studio...
另一个兴趣点 - 如果您将“MySQL 数据提供程序”条目添加到本地 machine.config(在我的情况下为 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config),您就可以连接通过 Visual Studio 到您的 MySql 实例...
回答by AlexC
This question and answer has been very useful for me migrating a larger EF project from SQL to mySQL so I thought I would add my notes and hope they are useful:
这个问题和答案对我将一个更大的 EF 项目从 SQL 迁移到 mySQL 非常有用,所以我想我会添加我的笔记并希望它们有用:
As noted the name of the connection string has to match the name of the Class which extends the System.Data.Entity.DbContext.
如前所述,连接字符串的名称必须与扩展 System.Data.Entity.DbContext 的类的名称相匹配。
There seems still no way to create tables in EF using the mySQL connector but you can use and modify the SQL create scripts to generate the mySQL tables. The easiest way I found to do this was to comment in and out the OnModelCreating function on the extended DbContext depending on whether the code was needed to recreate tables. If I find I am doing this more often I plan to resolve this by using dependency injection and have seperate classes based on either a mySQL or MSSQL configuration.
似乎仍然无法使用 mySQL 连接器在 EF 中创建表,但您可以使用和修改 SQL 创建脚本来生成 mySQL 表。我发现这样做的最简单方法是根据是否需要重新创建表的代码在扩展的 DbContext 上注释和注释 OnModelCreating 函数。如果我发现我经常这样做,我计划通过使用依赖注入来解决这个问题,并基于 mySQL 或 MSSQL 配置拥有单独的类。
I found it easier to make sure that the dev boxes and servers had the correct mySQL connector .dll packaged in the release than mess with the DbFactoryProviders in the webconfig. Getting the packaging correct in the project/solution build package meant I only needed the connection string lines and not the DbFactoryProviders lines which I found to be difficult to work consistently across a number of machines.
我发现确保开发箱和服务器在发行版中打包了正确的 mySQL 连接器 .dll 比在 webconfig 中弄乱 DbFactoryProviders 更容易。在项目/解决方案构建包中正确打包意味着我只需要连接字符串行而不是 DbFactoryProviders 行,我发现它很难在许多机器上一致地工作。
I needed to change the mySQL Identifier Case Sensitivity from the setting I had of 0 to 1. Without this setting the SQL that EF connected could not find the tables that were there due to the mixed case names of my objects compared to the fixed case tables that mySQL creates.
我需要将 mySQL Identifier Case Sensitivity 从我的设置 0 更改为 1。如果没有这个设置,EF 连接的 SQL 将无法找到存在的表,因为与固定大小写表相比,我的对象的大小写混合mySQL 创建的。