使用MySql与实体框架4和代码优先开发CTP [英] Using MySql with Entity Framework 4 and the Code-First Development CTP

查看:145
本文介绍了使用MySql与实体框架4和代码优先开发CTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我会尝试一下Scott Guthrie的最新的帖子在使用实体框架的代码优先开发4.而不是使用Sql Server,我试图使用MySql。以下是我的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数据提供者
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数据库不存在。



足够公平我去创建了MySql数据库,只是为了看事情是否正常工作,并且得到另一个ProviderIncompatibleException。这一次,DatabaseExists不受提供者的支持。



我承认,这是我第一次深入实体框架(我已经大部分停留在Linq到Sql),而这一切都是在上周发布的Code-First CTP上运行的。那么说,我在这里做错什么,还是一个可以解决的问题?

解决方案




  • 无法创建数据库,必须已经存在

  • 您必须使用DBContext名称为每个DB比赛创建一个连接字符串(在上面的示例中,连接字符串必须以NerdDinners的名字存在),而不仅仅是默认值(否则将使用SQL)

  • 它将使用您用于定义上下文的DBSet名称作为表的名称,因此在命名时要小心。



总而言之,一条漫长的道路,但到最后



**更新
另外需要注意的是,部署时您最喜欢使用MySQL的MVC网站,也需要在您的web.config中添加一个DataFactory。
通常是因为MySql连接器的差异以及支持的MySQL版本。
(很多头部划痕后通过其他来源发现的答案)
只需添加:

 <系统。数据> 
< DbProviderFactories>
< add name =MySQL数据提供者
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 DLLs以确保兼容性。


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>

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.

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

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.

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

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:

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

这篇关于使用MySql与实体框架4和代码优先开发CTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆