我有一個使用EF6的應用程式。它同時支持 SQL Server 和 MySql,并且在運行時應用了適當的設定(如果設定為 MySql,則使用 MySqlEFConfiguration,等等)。
對于 MySql,一切都作業得完美無缺。
在使用 SQL Server 時,會發生以下情況:
在使用 SQL Server 時,會發生以下情況。
- 連接建立成功。
- 呼叫
context.Database.Initialize()的結果是對最新的MigrationId等進行所有正常的查詢,如果資料庫/表不存在,它將成功創建它們。 - 然后,它崩潰了,出現了以下例外: 。
指定的chema是無效的。 錯誤:
(0,0) : 錯誤 0175: ADO.NET提供者的不變名稱為'MySql.Data.MySqlClient',要么沒有在機器或應用程式組態檔中注冊,要么不能被加載。詳情請看內部例外。
它說詳細情況見內部例外,但內部例外是空的。
我到處尋找,并嘗試無休止地擺弄組態檔(提供者定義等),但每次都是如此。
我曾試圖在一個干凈的專案中重現這個問題,但沒有成功,它總是按照預期作業。
為什么它試圖使用MySqlClient?
編輯:
配置代碼沒有什么特別的,只是一個
if (Settings.Default.databaseType == "MySql"/span>)
{
DbConfiguration.SetConfiguration(newMySqlEFConfiguration())。
我已經驗證了在使用SQL Server時沒有呼叫這個。連接字串也是在運行時設定的,我也驗證了這些。所有的配置代碼都被復制到干凈的專案中,試圖重現這個問題,但我還沒有成功地觸發它。
相關配置部分:
<system.data>
<DbProviderFactories>
<add name="SqlClient Data Provider" invariant="System.Data。 SqlClient" description="。 用于SqlServer的Net框架資料提供者" type="System.Data.SqlClient.SqlClientFactory, System.Data" />
</DbProviderFactories>
</system.data>/span>
<entityFramework>
<providers>/span>
<provider invariantName="System.Data。 SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/span> />
<provider invariantName="MySql.Data。 MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />/span>
</entityFramework>/span>
<connectionStrings>/span>
<clear />>
< add name="mydbcontext" connectionString=" providerName=" />
</connectionStrings>
編輯2:這里是呼叫堆疊,如果它有任何幫助的話。據我所知,由于某些原因,DefaultProviderFactoryResolver給出了錯誤的答案。但為什么呢?使用一個自定義的ProviderFactoryResolver沒有任何影響。
EntityFramework.dll! <>c.AnonymousMethod(System.ArgumentException e = {unknown}, string n = {unknown}) C# 非用戶代碼
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(System.Type type = {unknown}, object key = {unknown}, System. Func<System.ArgumentException,string,object> handleFailedLookup = {unknown}) C# Non-user code
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(System.DefaultProviderFactoryResolver)。 Type type = {unknown}, object key = {unknown}) C# 非用戶代碼
EntityFramework.dll! <>c__DisplayClass4_0.AnonymousMethod(System.Tuple<System. Type,object> k = {unknown}) C# Non-user code!
mscorlib.dll!System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key = {unknown}, System.Func valueFactory = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(System.Data.Entity.Infrastructure.CachingDependencyResolver)。 Type type = {unknown}, object key = {unknown}) C#非用戶代碼
EntityFramework.dll!<>c__DisplayClass5_0.AnonymousMethod(System.Data.Entity.Infrastructure.DependencyResolution.IDbDependencyResolver r = {unknown}) C# 非用戶代碼。
System.Core.dll! WhereSelectArrayIterator`2.MoveNext() C# 非用戶代碼。
System.Core.dll!System.Linq.Enumerable.FirstOrDefault(System.Collections.Generic.IEnumerable source = {unknown}, System.Func predicate = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain)。 Type type = {unknown}, object key = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(System.Data.Entity.Infrastructure.RootDependencyResolver)。 Type type = {unknown}, object key = {unknown}) C#非用戶代碼
EntityFramework.dll!<>c__DisplayClass5_0.AnonymousMethod(System.Data.Entity.Infrastructure.DependencyResolution.IDbDependencyResolver r = {unknown}) C# 非用戶代碼。
System.Core.dll! WhereSelectArrayIterator`2.MoveNext() C# 非用戶代碼。
System.Core.dll!System.Linq.Enumerable.FirstOrDefault(System.Collections.Generic.IEnumerable source = {unknown}, System.Func predicate = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain)。 Type type = {unknown}, object key = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(System.Data.Entity.CompositeResolver)。 Type type = {unknown}, object key = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService(System.Data.Entity.Infrastructure.DependencyResolution。 IDbDependencyResolver resolver = {unknown}, object key = {unknown}) C# Non-user code.
EntityFramework.dll!Loader.InitializeProviderManifest(System.Action<string,System.Data.Entity.Core.SchemaObjectModel.ErrorCode,System.Data.Entity.Core.Metadata.Edm.EdmSchemaErrorSeverity> addError = {unknown}) C# 非用戶碼
EntityFramework.dll!Loader.OnProviderManifestTokenNotification(string token = {unknown}, System.Action<string,System.Data.Entity.Core.SchemaObjectModel.ErrorCode,System.Data.Entity.Core.Metadata.Edm.EdmSchemaErrorSeverity> addError = {unknown}) C# 非用戶碼
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.Schema.HandleProviderManifestTokenAttribute(System.Xml.XmlReader reader = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.Schema.HandleAttribute(System.Xml.XmlReader reader = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.SchemaElement.ParseAttribute(System.Xml.XmlReader reader = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.SchemaElement.Parse(System.Xml.XmlReader reader = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.Schema.HandleTopLevelSchemaElement(System.Xml.XmlReader reader = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.Schema.InternalParse(System.Xml.XmlReader sourceReader = {unknown}, string sourceLocation = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.Schema.Parse(System.Xml.XmlReader sourceReader = {unknown}, string sourceLocation = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(System.Collections.Generic.IEnumerable<System.Xml.XmlReader> xmlReaders = {unknown},System. Generic.IEnumerable<string> sourceFilePaths = {unknown}, System.Data.Entity.Core.SchemaObjectModel.SchemaDataModelOption dataModel = {unknown}, System.Data.Entity.Core.SchemaObjectModel. AttributeValueNotification providerNotification = {unknown}, System.Data.Entity.Core.SchemaObjectModel.AttributeValueNotification providerManifestTokenNotification = {unknown}, System.Data.Entity.Core.SchemaObjectModel.TokenNotification = {unknown}. ProviderManifestNeeded = {unknown}, out System.Collections.Generic.IList<System.Data.Entity.Core.SchemaObjectModel.Schema> schemaCollection = {unknown}) C#非用戶代碼。
EntityFramework.dll!Loader.LoadItems(System.Collections.Generic.IEnumerable<System.Xml.XmlReader> xmlReaders = {unknown}, System. Generic.IEnumerable<string> sourceFilePaths = {unknown}) C# Non-user code。
EntityFramework.dll!Loader..ctor(System.Collections.Generic.IEnumerable<System.Xml.XmlReader> xmlReaders = {unknown}, System.Collections.Generic. IEnumerable<string> sourceFilePaths = {unknown}, bool throwOnError = {unknown}, System.Data.Entity.Infrastructure.DependencyResolution.IDbDependencyResolver resolver = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Init(System.Collections.Generic.IEnumerable<System.Xml.XmlReader> xmlReaders = {unknown}, System.Collections.Generic. IEnumerable<string> filePaths = {unknown}, bool throwOnError = {unknown}, System.Data.Entity.Infrastructure.DependencyResolution.IDbDependencyResolver resolver ={unknown}, out System. Data.Entity.Core.Common.DbProviderManifest providerManifest = {unknown},out System.Data.Common.DbProviderFactory providerFactory = {unknown},out string providerInvariantName = {unknown},out string providerManifestToken = {unknown},out System.Data.Entity.Core.Common.DbProviderToken = {unknown }。 Data.Entity.Core.Common.Utils.Memoizer<System.Data.Entity.Core.Metadata.Edm.EdmFunction,System.Data.Entity.Core.Metadata.Edm.EdmFunction> cachedCTypeFunction = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Core.Metadata.Edm.StoreItemCollection..ctor(System.Collections.Generic.IEnumerable<System.Xml.XmlReader> xmlReaders = {unknown}) C# 非用戶代碼
EntityFramework.dll!System.Data.Entity.Utilities.XDocumentExtensions.GetStorageMappingItemCollection(System.Xml.Linq.XDocument model = {unknown}, out System.Data.Entity.Infrastructure.DbProviderInfo providerInfo = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(System.Xml.Linq.XDocument sourceModel = {unknown}, System.Xml.Linq.XDocument targetModel = {unknown}, System.Lazy<System.Data.Entity.Migrations.Infrastructure。 ModificationCommandTreeGenerator> modificationCommandTreeGenerator = {unknown}, System.Data.Entity.Migrations.Sql.MigrationSqlGenerator migrationSqlGenerator = {unknown}, string sourceModelVersion = {unknown}, string targetModelVersion = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(System.Xml.Linq.XDocument model = {unknown}, System.Data.Entity.Migrations.DbMigration lastMigration = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Migrations.DbMigrator.ApplyMigration(System.Data.Entity.Migrations.DbMigration migration = {unknown}, System.Data.Entity.Migrations.DbMigration lastMigration = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Migrations.DbMigrator.Upgrade(System.Collections.Generic.IEnumerable<string> pendingMigrations = {unknown}, string targetMigrationId = {unknown}, string lastMigrationId = {unknown}) C# Non-user code
EntityFramework.dll!System.Data.Entity.Migrations.DbMigrator.UpdateInternal(string targetMigration = {unknown}) C#非用戶代碼。
EntityFramework.dll! <>c__DisplayClass42_0.AnonymousMethod() C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(System.Action mustSucceedToKeepDatabase = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Migrations.DbMigrator.Update(string targetMigration = {unknown}) C# 非用戶代碼。
EntityFramework.dll! System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context = {unknown}) C# Non-user code
EntityFramework.dll!<>c__DisplayClass66_0`1.AnonymousMethod() C# Non-user code
EntityFramework.dll!System.Data.Entity.Internal.InternalContext.PerformInitializationAction(System.Action action = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() C# 非用戶代碼。
EntityFramework.dll! <>c.AnonymousMethod(System.Data.Entity.Internal.InternalContext c = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input = {unknown}) C#非用戶代碼
EntityFramework.dll!System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(System.Action<System.Data.Entity.Internal.InternalContext> action = {unknown}) C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() C# 非用戶代碼。
EntityFramework.dll!System.Data.Entity.Internal.InternalContext.Initialize() C# 非用戶代碼。
EntityFramework.dll! System.Data.Entity.Database.Initialize(bool force = {unknown}) C# 非用戶代碼。
uj5u.com熱心網友回復:
最后通過添加:
解決了這個問題 <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
到DbProviderFactories配置部分。為什么在使用SQL Server時需要這樣做,而在使用MySql時則不需要?我沒有絲毫的想法。
uj5u.com熱心網友回復:
你需要對SqlServer和MySql進行不同的配置。下面是我的個人實作。
首先,我創建了一個擴展了DbContext類的新類。這有助于保持事物的分離和組織
public class MySqlContext : DbContext
{
public MySqlContext(DbContextOptions)
: base(options)
{[/span>
}
}
然后我創建了一個新的Extentions類,它再次幫助一切保持有序。它也是通用的,所以我可以輕松地添加多個背景關系。
注意下面我使用了UseSqlServer,然而,或者Mysql你將需要使用UseMySQL。你可能需要為此安裝軟體包。
public static class ServiceCollectionExtentions
{
// <summary>
// 添加SQL語境
// </summary>/span>
// <param name="services"></param>
// <param name="options"></param>
// <returns></returns>
public static IServiceCollection AddContext<TContext> (this IServiceCollection services, string connectionstring, ServiceLifetime serviceLifetime = ServiceLifetime. string connectionstring, serviceLifetime serviceLifetime = ServiceLifetime.) where TContext : DbContext => services
.AddDbContext<TContext>(options => options.UseSqlServer( // You can replace this with `UseMySQL`/span>
連接字串。
// 通過這個我改變了遷移的生成位置。
//這只是我用于資料訪問層的專案。
actions => actions.MigrationsAssembly("<Your Project Namespace>")
.EnableRetryOnFailure()
), serviceLifetime)。
}
下面是一個用法示例:
public void ConfigureServices(IServiceCollection services)
{
//>添加SQL資料庫連接。
services.AddContext<MySqlContext>(Configuration.GetConnectionString("SqlConnection") )。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321954.html
標籤:
