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

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

问题描述

我想我会尝试与斯科特Guthrie的一点<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/07/16/$c$c-first-development-with-entity-framework-4.aspx\">latest帖子在code-先开发与实体框架4而不是使用SQL Server,我试图使用MySQL。这里是我的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代替。这一次,

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到SQL),这是在code-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名称(在上面的示例中的ConnectionString必须名为NerdDinners的存在),每个DB大赛的连接字符串,而不仅仅是一个默认的(否则它将使用SQL)

  • 它将使用您用来定义上下文表的名称DBSet名的名字,命名它们时要小心。

所有的一切,一条漫长的道路,但到底有

All in all, a long road but there in the end

**更新
还有一点要注意,使用MySQL你最喜欢的需求也加入到的DataFactory你的web.config部署MVC网站的时候。
通常是因为在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.

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

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