简体   繁体   English

在实体框架中从SQL Server告诉MySQL

[英]Telling MySQL from SQL Server in Entity Framework

I understand this question looks odd because EF takes care of LINQ to SQL translation, but I'm maintaining an application in which queries are hardcoded and encrypted in the database itself (and written by a lawyer who certifies their correctness). 我知道这个问题看起来很奇怪,因为EF负责LINQ到SQL的转换,但是我正在维护一个应用程序,其中查询在数据库本身中 (由证明其正确性的律师编写) 进行硬编码和加密

Basically I can fire the SQL text using DbContext.Database.Connection object, but these queries are written in a dialect-agnostic format. 基本上,我可以使用DbContext.Database.Connection对象触发SQL文本,但是这些查询是以与方言无关的格式编写的。 So the code performs a token substitution based on an if case bound to a configuration variable 因此,代码根据绑定到配置变量的if大小写执行令牌替换

if (Configuration.DbType == DbType.MYSQL)
    query = ReplaceForMySQL(query);
if (Configuration.DbType == DbType.SQLSERVER)
    query = ReplaceForSqlServer(query);

I can either leave the code as it is or get rid of the DbType parameter from the Web.config . 我可以保留代码DbTypeWeb.config 删除 DbType参数。

DB is bound using EF connection string. 使用EF连接字符串绑定数据库。 How can I tell what DBMS is currently in use? 我如何知道当前正在使用什么DBMS? For example, in NHibernate I could test the dialect from the ISessionFactory or the Configuration 例如,在NHibernate中,我可以从ISessionFactoryConfiguration测试方言

The shortest way would be 最短的方法是

context.Database.Connection.GetType().Name

where context is a DbContext instance. 其中context是一个DbContext实例。 This gives you the actual type of the DbConnection that's used by EF, from which you can easily tell which provider is in play. 这为您提供了EF使用的DbConnection的实际类型,您可以从中轻松判断正在使用哪个提供程序。

(If this is correct please have it certified with your lawyer ツ) (如果正确,请向您的律师certified证明)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM