期货ctp mysql,使用MySql与实体框架4和代码优先开发CTP

本文介绍了尝试使用Scott Guthrie的Entity Framework 4代码优先开发方式与MySQL数据库配合时遇到的问题,包括数据库创建失败和提供者不兼容异常。解决方法涉及预先创建数据库、为每个DbContext指定连接字符串以及在部署MVC站点时添加数据工厂到web.config中。需要注意数据库名称、DBSet命名以及MySQL连接库的版本匹配。
摘要由CSDN通过智能技术生成

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

connectionString="Server=localhost; Database=NerdDinners; Uid=root; Pwd=;"

providerName="MySql.Data.MySqlClient"/>

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

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:

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值