如何改变数据库 - 通过架构实体框架(mysql数据库)使用吗? [英] How to change the database - Schema used by Entity Framework (mysql database)?

查看:130
本文介绍了如何改变数据库 - 通过架构实体框架(mysql数据库)使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的EntityFramework项目中的连接到MySQL数据库。该供应商是Devart Dot.connect。

I use EntityFramework in a project to connect to a Mysql database. The provider is Devart Dot.connect.

本应用程序需要连接到使用由用户在运行时给定的联接参数的数据库。这当然也包括MySQL数据库的名称。

This application needs to connect to a database using connexion parameters given by the user at runtime. This includes of course the Mysql Database name.

我用EntityConnectionStringBuiler和EntityConnection类积聚和存储自定义参数联接。

I used the EntityConnectionStringBuiler and EntityConnection classes to build-up and store the custom connexion parameters.

的问题是,即使有这样的给定的参数,该应用程序总是连接至设计EntityModel使用视觉工作室向导时命名数据库。
什么是很奇怪的是,debuging和检查ObjectContext中的地位时,自定义参数联接正确使用...
这让我疯了!!!!!
任何线索?

The problem is that even with such given parameters, the application always connect to the database named when designing the EntityModel using the visual studio wizard. What is very strange is that when debuging and checking the status of the ObjectContext, the custom connexion parameters are correctly used... It makes me mad !!!!! Any clue ?

推荐答案

在这个问题上花费一天后,我终于明白了,问题来了从model.edmx文件。

After spending one day on this issue, I finally came to understand that the problem was coming from the model.edmx file.

在这个文件中,你必须每EntitySet的一条线。
上的每个元素的EntitySet有一个名为架构的属性。在SQL Server中将此属性设置为相关表模式时:

In this file, you have one line per EntitySet. On each EntitySet element there is an attribute called schema. In case of SQL Server this attribute is set to the related table schema :

EntitySet的名称=annee_civile
的EntityType = openemisModel.Store.annee_civile
店:TYPE =表模式=MyDatabase的/>

EntitySet Name="annee_civile" EntityType="openemisModel.Store.annee_civile" store:Type="Tables" Schema="mydatabase" />

如果您提供constructiong你自己EntityConnection时的模式的名字,它似乎有冲突,终于在EDMX文件中定义的架构将即使您指定一个又一个的连接参数使用。

If you provide the name of the Schema when constructiong you own EntityConnection, it seem that there is a conflict and that finally, the Schema defined in the edmx file will be used even if you specified another one in the connection parameters.

解决方法是删除在EDMX文件中的架构的名称。连接到SQL服务器时,
这个工程的MYSQL,可能不会。

The solution is simply to remove the name of the schema in the edmx file. THIS WORKS FOR MYSQL, probably not when connecting to a SQL server.

EntitySet的名称=annee_civile
的EntityType =openemisModel.Store.annee_civile
店:类型=表模式=/>

EntitySet Name="annee_civile" EntityType="openemisModel.Store.annee_civile" store:Type="Tables" Schema="" />

EntityConnectionStringBuilder:

The EntityConnectionStringBuilder :

字符串providedString =用户ID = XXXX;密码= XXX;主机= XXXX;数据库= anydatabasename;
EntityConnectionStringBuilder entityConnBuilder =新EntityConnectionStringBuilder();
entityConnBuilder.Provider =Devart.Data.MySql;
entityConnBuilder.Metadata = @RES:// /OpenEmisModel.csdl|res:// 的/OpenEmisModel.ssd​​l|res://*/OpenEmisModel.msl;
entityConnBuilder.ProviderConnectionString = providedString;

string providedString = "User Id=xxxx;Password=xxx;Host=xxxx;Database=anydatabasename"; EntityConnectionStringBuilder entityConnBuilder = new EntityConnectionStringBuilder(); entityConnBuilder.Provider = "Devart.Data.MySql"; entityConnBuilder.Metadata = @"res:///OpenEmisModel.csdl|res:///OpenEmisModel.ssdl|res://*/OpenEmisModel.msl"; entityConnBuilder.ProviderConnectionString = providedString;

该EntityConnection和使用它的对象上下文:

The EntityConnection and the object context using it:

EntityConnection entityConnexionEmis =新的EntityConnection(entityConnBuilder.ConnectionString);

EntityConnection entityConnexionEmis = new EntityConnection(entityConnBuilder.ConnectionString);

objectcontextEntities testingContext =新objectcontextEntities(entityConnexionEmis);

objectcontextEntities testingContext = new objectcontextEntities(entityConnexionEmis);

该软件现在能够连接到任何数据库名称。

The software is now able to connect to any database name.

希望这有助于。

这篇关于如何改变数据库 - 通过架构实体框架(mysql数据库)使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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