在实体框架 4 和代码优先开发 CTP 中使用 MySql

我想我会尝试一下 Scott Guthrie 的 最新文章 使用实体框架 4 进行代码优先开发.我尝试使用 MySql,而不是使用 Sql Server.这是我的 web.config 的相关部分(这是一个 Asp.Net MVC 2 应用程序):

I thought I'd experiment a bit with Scott Guthrie's latest post on 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):

<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>

就像教程一样,我希望 EF4 自动为我生成数据库.相反,它抛出一个 ProviderIncompatibleException,内部异常抱怨 NerdDinners 数据库不存在.

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.

很公平;我去为它创建了 MySql 数据库只是为了看看事情是否可行,并得到了另一个 ProviderIncompatibleException .这次,提供程序不支持DatabaseExists".

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".

我承认,这是我第一次真正深入研究实体框架(我主要坚持使用 Linq to Sql),而且这一切都在上周发布的 Code-First CTP 上运行.也就是说,我在这里做错了什么,或者是可以解决的已知问题吗?

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?

推荐答案

好的,终于有了几个兴趣点.

Right, finally got it working with a few points of interest.

  • 无法创建数据库,必须已经存在
  • 您必须使用 DBContext 名称为每个数据库竞赛创建一个连接字符串(在上面的示例中,连接字符串必须存在名称为NerdDinners"),而不仅仅是默认的(否则它将使用 SQL)
  • 它将使用您用来定义上下文的 DBSet 名称作为表的名称,因此命名它们时要小心.

总之,路漫漫其修远兮

**更新另一点要注意,当使用 MySQL 部署您的 MVC 站点时,您最喜欢还需要将 DataFactory 添加到您的 web.config.通常是因为 MySql 连接器和支持的 MySQL 版本不同.(经过多次摸索后通过其他来源找到的答案)只需添加:

**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:

  <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>

作为 web.config 的一个单独部分 确保设置与站点一起部署的 MySQL.Data.dll 的版本号(复制为本地"也是一个好主意)MySQL DLL 以确保兼容性.

As a seperate section to your web.config making sure to set the version number of the MySQL.Data.dll you deploy with the site (also a good idea to "copy as local" your MySQL DLLs to ensure compatibility.

相关文章