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

时间:2022-05-04 02:14:49

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

我想尝试一下Scott Guthrie关于代码优先开发和实体框架4的最新文章。我没有使用Sql Server,而是尝试使用MySql。以下是我网站的相关部分。配置(这是一个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自动为我生成db。相反,它抛出一个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 db只是为了看看它是否能工作,然后得到了另一个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到Sql),这都是在上周发布的代码第一CTP上运行的。也就是说,我在这里做错了什么,还是有一个已知的问题可以解决?

3 个解决方案

#1


25  

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

好吧,终于让它在几个有趣的点上工作了。

  • Cannot create a DB, must exist already
  • 无法创建DB,必须已经存在
  • 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)
  • 您必须使用DBContext名称为每个DB比赛创建一个连接字符串(在上面的示例中,一个connectionstring必须以“NerdDinners”的名称存在),而不仅仅是一个默认的字符串(否则它将使用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.
  • 它将使用您用来定义上下文的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站点时,您最喜欢的是在web.config中添加一个DataFactory。通常是因为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 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.

作为您的web的独立部分。配置确保设置MySQL.Data的版本号。与站点一起部署的dll(也可以将MySQL dll“作为本地”进行“复制”,以确保兼容性)。

#2


2  

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数据提供程序”条目添加到本地计算机中。配置(C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ config \机器。在我的情况下,你可以通过Visual Studio连接到你的MySql实例…

#3


2  

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 create脚本来生成mySQL表。我发现最简单的方法是在扩展的DbContext上对onmodelcreation函数进行注释,这取决于是否需要代码来重新创建表。如果我发现我经常这样做,我打算通过依赖项注入来解决这个问题,并根据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 connector .dll比在webconfig中处理dbfactoryprovider更容易。在项目/解决方案构建包中获得正确的打包意味着我只需要连接字符串行,而不需要dbfactoryprovider行,因为我发现在许多机器上很难持续地工作。

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标识符大小写敏感性从0改为1。如果没有这个设置,EF连接的SQL将无法找到存在的表,因为与mySQL创建的固定的case表相比,我的对象的大小写混合。

#1


25  

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

好吧,终于让它在几个有趣的点上工作了。

  • Cannot create a DB, must exist already
  • 无法创建DB,必须已经存在
  • 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)
  • 您必须使用DBContext名称为每个DB比赛创建一个连接字符串(在上面的示例中,一个connectionstring必须以“NerdDinners”的名称存在),而不仅仅是一个默认的字符串(否则它将使用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.
  • 它将使用您用来定义上下文的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站点时,您最喜欢的是在web.config中添加一个DataFactory。通常是因为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 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.

作为您的web的独立部分。配置确保设置MySQL.Data的版本号。与站点一起部署的dll(也可以将MySQL dll“作为本地”进行“复制”,以确保兼容性)。

#2


2  

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数据提供程序”条目添加到本地计算机中。配置(C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ config \机器。在我的情况下,你可以通过Visual Studio连接到你的MySql实例…

#3


2  

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 create脚本来生成mySQL表。我发现最简单的方法是在扩展的DbContext上对onmodelcreation函数进行注释,这取决于是否需要代码来重新创建表。如果我发现我经常这样做,我打算通过依赖项注入来解决这个问题,并根据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 connector .dll比在webconfig中处理dbfactoryprovider更容易。在项目/解决方案构建包中获得正确的打包意味着我只需要连接字符串行,而不需要dbfactoryprovider行,因为我发现在许多机器上很难持续地工作。

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标识符大小写敏感性从0改为1。如果没有这个设置,EF连接的SQL将无法找到存在的表,因为与mySQL创建的固定的case表相比,我的对象的大小写混合。