生成ADO.NET实体框架使用SQL Server 2008 R2和连接到MySQL [英] Generate ADO.NET Entity Framework using SQL Server 2008 R2 and Connect to MySQL

查看:191
本文介绍了生成ADO.NET实体框架使用SQL Server 2008 R2和连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发上的SQL Server数据库,并使用MySQL进行生产。

我已经创建了使用SSMS图我的数据库,在我的ASP.NET MVC项目产生的ADO.NET相应的实体模型。然后我修改连接字符串从SQL Server指向MySQL服务器,具体通过MySqlConnection对象到的DbContext。现在,我想下面code来执行这一点,但它抱怨说,提供基本的连接是.NET SQL Server连接类型不是。

  dbContext.CreateDatabase();
 

解决方案

在web.config中,你需要:

  • 为MySQL添加提供程序工厂(如上图所示)
  • 修改连接字符串(或创建一个新的),其中的ProviderName属性设置相应的(像<添加名称=NorthwindEntities的connectionString =元= NorthwindModel;供应商= MySQL数据提供者;提供连接字符串="数据源= \ SQLEx preSS;初始目录=罗斯文;集成安全性= TRUE"的providerName =System.Data.EntityClient/>

在你需要有两个SSDL文件中的这些变化之上 - 一个是SQL Server,另一个是MySQL。这些文件是专门针对你使用的存储。他们不仅用店里了解到的类型,但也有提供具体的信息(我把它放在{大括号},对不起,我不知道的示范提供商明证。我的SQL):

 <模式命名空间=NorthwindEFModel.Store别名=自我的xmlns =htt​​p://schemas.microsoft.com/ado/2009/11/edm/ssdl
    供应商={MySQL的数据提供}ProviderManifestToken ={提供商明证}>
 

我觉得你可以节省一些麻烦,如果您第一次使用code。您将创建并可能使用code(班+ DbContext.OnModelCreating())配置你的模型,并有EF创建数据库为您服务。根据您的连接字符串(在这种情况下将是一个正规的连接字符串,而不是实体连接字符串),它会说话要么到SQL Server或MySQL,并创建相应的数据库。

I want to develop on sql server database and use MySql for production.

I've created my database using SSMS Diagram and generated ADO.NET Enity Model in my ASP.NET MVC project. Then I modified connection string from Sql Server to point to MySql Server and specifically passed MySqlConnection object to DbContext. Now I'm trying to execute this below code but it complains that provided underlying connection is not of .NET Sql Server Connection type.

dbContext.CreateDatabase();

解决方案

In the web.config you need to:

  • add the provider factory for MySQL (as shown above)
  • modify your connection string (or create a new one) where providerName attribute is set accordingly (something like <add name="NorthwindEntities" connectionString="metadata=NorthwindModel;provider=MySQL Data Provider;provider connection string="Data Source=.\SQLExpress;Initial Catalog=Northwind;Integrated Security=True"" providerName="System.Data.EntityClient"/>

On top of these changes you need to have two SSDL files - one that is for Sql Server and one that is for MySQL. These files are specific to the store you use. They not only use the types the store understands but also have provider specific information (I put it in the {curly brackets}, sorry I don't know an exemplary provider manifest token for my Sql):

<Schema Namespace="NorthwindEFModel.Store" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"
    Provider="{MySQL Data Provider}" ProviderManifestToken="{Provider manifest token}">

I think you can save some hassle if you use code first. You will create and possibly configure your model using code (classes + DbContext.OnModelCreating()) and have EF create the database for you. Depending on your connection string (which in this case will be a "regular" connection string and not Entity Connection String) it will be talking either to Sql Server or MySQL and will create database accordingly.

这篇关于生成ADO.NET实体框架使用SQL Server 2008 R2和连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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