如何更改数据库 - 实体框架(mysql 数据库)使用的模式? [英] How to change the database - Schema used by Entity Framework (mysql database)?

查看:27
本文介绍了如何更改数据库 - 实体框架(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.

问题在于,即使使用这样的给定参数,应用程序总是连接到使用 Visual Studio 向导设计 EntityModel 时指定的数据库.很奇怪的是,在调试和检查ObjectContext的状态时,自定义的connexion参数都被正确使用了……这让我很生气!!!!有什么线索吗?

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 元素上都有一个名为 schema 的属性.在 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 Name="annee_civile";EntityType=openemisModel.Store.annee_civile"商店:类型=表";架构=我的数据库";/>

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

如果您在构建自己的EntityConnection时提供了Schema的名称,似乎有冲突,最后,即使您在连接参数中指定了另一个,也会使用edmx文件中定义的Schema.

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 文件中的架构名称.这适用于 MYSQL,但在连接到 SQL 服务器时可能不适用.

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 Name="annee_civile";EntityType=openemisModel.Store.annee_civile"商店:类型=表";架构="/>

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

EntityConnectionStringBuilder :

The EntityConnectionStringBuilder :

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 = 提供的字符串;

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 = new EntityConnection(entityConnBuilder.ConnectionString);

EntityConnection entityConnexionEmis = new EntityConnection(entityConnBuilder.ConnectionString);

objectcontextEntities testingContext = new objectcontextEntities(entityConnexionEmis);

objectcontextEntities testingContext = new objectcontextEntities(entityConnexionEmis);

该软件现在可以连接到任何数据库名称.

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

希望这会有所帮助.

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

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