错误 0175:在配置中找不到指定的商店提供程序,或者无效
我正在使用实体框架代码优先迁移,并且在本地一切都很好。
我的数据库是 SQLServer 2008。(也使用 SQL CE 进行本地开发)我可以毫无问题地在本地使用实时 SQL Server db 连接字符串。
这是 _appStart.cshtml 中的迁移代码:
@using EF = System.Data.Entity;
@using TCommon = Told.Web.Common.Logic;
@{
// Manually initialize
var initializer = new EF.MigrateDatabaseToLatestVersion<TCommon.CommonContext, TCommon.Migrations.Configuration>();
System.Data.Entity.Database.SetInitializer(initializer);
initializer.InitializeDatabase(new TCommon.CommonContext());
// DB Maintenance
TCommon.AppUserLogic.CleanDatabase();
// Asp.Net
WebSecurity.InitializeDatabaseConnection("ToldDB", "UserProfile", "UserId", "Email", true);
// Common
TCommon.AppStart.Run();
}
这在当地很有效。无论是针对 SQL CE 还是针对我的实时数据库。它在本地运行良好,可以将我的实时数据库迁移到最新版本。
但是,当我把它放在我的网络服务器上时,它会抛出这个异常:
例外
Server Error in '/' Application.
Schema specified is not valid. Errors:
(0,0) : error 0175: The specified store provider cannot be found in the configuration, or is not valid.
[MetadataException: Schema specified is not valid. Errors:
(0,0) : error 0175: The specified store provider cannot be found in the configuration, or is not valid.]
System.Data.Metadata.Edm.Loader.ThrowOnNonWarningErrors() +8566285
System.Data.Metadata.Edm.Loader.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths) +181
System.Data.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerManifestToken, Memoizer`2& cachedCTypeFunction) +211
System.Data.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders) +295
System.Data.Entity.Migrations.Extensions.XDocumentExtensions.GetStoreItemCollection(XDocument model, DbProviderInfo& providerInfo) +180
System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString) +48
System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration) +55
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +269
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +456
System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +64
现在,这与迁移尝试有关。因此,当我将迁移调用包装在 try-catch 块中时,我可以很好地使用数据库(因为我在本地迁移了实时数据库)。
@using EF = System.Data.Entity;
@using TCommon = Told.Web.Common.Logic;
@{
// Manually initialize
var initializer = new EF.MigrateDatabaseToLatestVersion<TCommon.CommonContext, TCommon.Migrations.Configuration>();
System.Data.Entity.Database.SetInitializer(initializer);
try
{
initializer.InitializeDatabase(new TCommon.CommonContext());
}
catch (Exception ex)
{
}
var db = new TCommon.CommonContext();
var test = db.AppUsers.FirstOrDefault();
// DB Maintenance
TCommon.AppUserLogic.CleanDatabase();
// Asp.Net
WebSecurity.InitializeDatabaseConnection("ToldDB", "UserProfile", "UserId", "Email", true);
// Common
TCommon.AppStart.Run();
}
当我尝试在我的 Web 服务器上运行迁移差异检查时,什么会导致它失败?
编辑:
这是实体框架的 web.config:
<configuration>
<configSections>
...
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
...
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
</entityFramework>
<system.web>
<trace enabled="true" requestLimit="40" localOnly="false" pageOutput="true" />
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
...
<connectionStrings>
<add name="ToldDB" connectionString="..." providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>