我有一个使用 EntityFramework 和 SQL Server EF DB 的简单 ASP.NET 应用程序。
问题是,如果我尝试使用 SQL CE(精简版),我的应用程序服务会在第二次调用存储库时引发异常并出现错误:
ExecuteReader 需要一个开放且可用的 Connection。
如果我返回 SQL Express 数据库,一切都会成功。
这是我使用的步骤和代码:
修改客户端应用程序 App.config 文件(我只报告更改):
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" .../>
</configSections>
<connectionStrings>
<add name="Default" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=C:\temp\Deeds.sdf" />**
</connectionStrings>
...
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
...
</configuration>
他们我必须设置 UOW 的隔离级别:
public class DeedsWinFormEFModule : AbpModule {
public override void PreInitialize() {
Configuration.UnitOfWork.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
}
public override void Initialize() {
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
但是在我的 ApplicationService 中,此方法在第二个数据库查询中失败
public class DeedImmobileAppService
: DeedsAppServiceBase, IDeedImmobileAppService {
protected readonly IRepository<CfgBase100Repo> _cfgBase100Repo;
protected readonly IRepository<CfgCasa> _cfgCasaRepo;
...
public async Task<IReadOnlyList<DeedEntryDto>> CalculateValuesForEntries(UpdateDefaultEntriesRequest input) {
var tipoCasa = await _cfgCasaRepo.FirstOrDefaultAsync(input.HouseTypeId);
var base100 = await _cfgBase100Repo.FirstOrDefaultAsync(cfg => (cfg.MinDeedRange <= input.DeedValue) && (input.DeedValue < cfg.MaxDeedRange));
...
return list;
}
}
对存储库的第二次调用总是失败(var tipoCasa = ...
):
var base100 = await _cfgBase100Repo.FirstOrDefaultAsync(cfg => (cfg.MinDeedRange <= input.DeedValue) && (input.DeedValue < cfg.MaxDeedRange));
var tipoCasa = await _cfgCasaRepo.FirstOrDefaultAsync(input.HouseTypeId);
我还尝试反转线条。在这种情况下var base100 =
失败:
var tipoCasa = await _cfgCasaRepo.FirstOrDefaultAsync(input.HouseTypeId);
var base100 = await _cfgBase100Repo.FirstOrDefaultAsync(cfg => (cfg.MinDeedRange <= input.DeedValue) && (input.DeedValue < cfg.MaxDeedRange));
我收到的错误是:
Inner Exception 1:
EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.
Inner Exception 2:
InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is Closed.
这里是堆栈跟踪:
System.Reflection.TargetInvocationException HResult=0x80131604
Message=异常已被调用的目标抛出。
Source=mscorlib StackTrace:在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 在 System.Reflection.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System .Delegate.DynamicInvokeImpl(Object[] args) 在 System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) 在 System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext 的 ContextCallback 回调,对象状态,布尔值 preserveSyncCtx)。在 System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) 在 System.Windows.Forms.Control.InvokeMarshaledCallbacks() 处运行(ExecutionContext executionContext,ContextCallback 回调,对象状态)
在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows .Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(Form mainForm) 在 GPSoftware.Deeds.WinForm.Program.Main() 在 C:\ WA\GPse\NotuleNotai\Notule\Deeds.WinForm\Program.cs:第 34 行
内部异常1:
EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常。
内部异常 2:
InvalidOperationException:ExecuteReader 需要打开且可用的连接。连接的当前状态是关闭。