积极答复者
请问Entity Framework如何才能支持多数据库

问题
-
因为一个项目中需要支持多种不同类型的数据库,所以数据层选用EF框架。
例如,有如下代码:
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();// Set the provider name.
entityBuilder.Provider = providerCode;
其中providerCode即对应不同的数据库类型,我计划是放在配置文件中指定,可能是System.Data.SqlClient,也有可能是MySql.Data.MySqlClient,分别对应SQL Server和MySQL数据库,而两个数据库中的表结构完全一样。但是使用后发现,通过VS生成的Entity文件(edmx)中仍然带有数据库类型信息,用记事本打开edmx文件,发现有如下这段代码:
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="BusinessModel.Store" Alias="Self" Provider="MySql.Data.MySqlClient" ProviderManifestToken="5.0" ...
这样导致初始化数据库对象时(BusinessEntities businessDB = new BusinessEntities(connectionString))出现以下错误提示:
The 'MySql.Data.MySqlClient' provider from the specified SSDL artifact(s) does not match the expected 'System.Data.SqlClient' provider from the connection string.
请问应如何处理?谢谢
答案
全部回复
-
谢谢回复。
不过现在发现一个新的问题,有段代码在SQL Server中执行是正常的,但是在MySQL中报错,代码如下:
// 以下这段为查询,在调试时,代码执行到此处一切正常,recordList对应的结果视图也是有记录的。
IQueryable<vew_Resource> recordList = from record in SecurityDB.tbl_Resource
select new vew_Resource
{
fLanguageCode = record.fLanguageCode,
fResourceID = record.fResourceID,
fResourceText = record.fResourceText,
fDescription = record.fDescription,
fLanguageName = SecurityDB.tbl_CodeDetail.Where(r => r.fCodeValue == record.fLanguageCode).Select(r => r.fCodeName).FirstOrDefault()
};
// 此处报错,提示“Specified method is not supported”
int count = recordList.Count();
具体错误信息如下:在 System.NotSupportedException 中第一次偶然出现的“MySql.Data.Entity.dll”类型的异常
在 System.Data.EntityCommandCompilationException 中第一次偶然出现的“System.Data.Entity.dll”类型的异常测试发现,如果我把查询语句中的子查询注释掉,则代码可以执行正常,难道MySQL的Entity Framework不支持子查询语句吗?
请问大家有无碰到过类似问题,应如何处理?谢谢。