开发者

Entity Framework database first with MySQL and MS SQL

I currently have an application which uses EF database first which was built against SQL Server database. I would like to allow the ability to connect to either MSSQL or MySQL.

I assumed that by using EF I could just change the connection string to point to MySQL, however, when I do that I get the following error:

Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to type 'System.Data.SqlClient.SqlC开发者_如何转开发onnection'.

What other steps are needed to make EF work with MySQL and MSSQL.

Update: Connection String:

<add name="EntityConnString" connectionString="metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=MySql.Data.MySqlClient;
provider connection string=&quot;server=localhost;User Id=root;password=password;Persist Security Info=True;database=mydb;Connection Protocol=NamedPipe&quot;" providerName="System.Data.EntityClient" />


You need separate EDMX for that - or at least SSDL part and most probably also MSL part specified in the connection string. The reason is that SSDL (storage description in EDMX) is provider dependent = at the moment your SSDL says: "I can connect only to MSSQL". If you want to support MySQL you need SSDL which will say: "I can connect only to MySQL" and define the connection string accordingly to used database server. SSDL contains database specific definitions. If your MySQL database will use different names of tables or columns or different schema you will also need a new MSL part (describing mapping between database tables and your entities).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜