为什么更新记录时亚音速和ActiveRecord的抛出异常? [英] Why is SubSonic and ActiveRecord throwing an exception when updating a record?
问题描述
我在MS SQL Server 2008标准版下表:
I have the following table in MS SQL Server 2008 Standard Edition:
CREATE TABLE [dbo].[NewTestQueue](
[JobID] [int] IDENTITY(1,1) NOT NULL,
[ServerName] [varchar](50) NULL,
[DomainID] [int] NOT NULL,
[Action] [varchar](50) NULL,
[Folder] [varchar](150) NULL,
[Method] [varchar](50) NULL,
[ActionProfile] [varchar](50) NULL,
[Title] [varchar](150) NULL,
[Suffix] [varchar](50) NULL,
[Host] [varchar](150) NULL,
[Url] [varchar](250) NULL,
[Expression] [varchar](50) NULL,
[MasterTest] [varchar](50) NULL,
[Completed] [bit] NOT NULL
) ON [PRIMARY]
我使用的是亚音速的ActiveRecord T4模板,并具有下列code:
I'm using the SubSonic ActiveRecord T4 template and have the following code:
var tests = NewTestQueue.Find(d => !d.Completed);
foreach(var test in tests)
{
// Do some work with test
// ...
// Now mark job as completed
test.Completed = true;
// System.NullReferenceException thrown here
test.Update();
}
引发的异常是:
The exception thrown is:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="SubSonic.Core"
StackTrace:
at SubSonic.Extensions.Database.ToUpdateQuery[T](T item, IDataProvider provider)
at SubSonic.Repository.SubSonicRepository`1.Update(T item, IDataProvider provider)
at HostMonitor.NewTestQueue.Update(IDataProvider provider) in E:\AppsDev.NET\_UK_Minds\Tollon Components\HostMonitor\Tollon.HostMonitor.TestGenerator\ActiveRecord\ActiveRecord.cs:line 593
at HostMonitor.NewTestQueue.Update() in E:\AppsDev.NET\_UK_Minds\Tollon Components\HostMonitor\Tollon.HostMonitor.TestGenerator\ActiveRecord\ActiveRecord.cs:line 586
at Tollon.HostMonitor.TestGenerator.Program.Main(String[] args) in E:\AppsDev.NET\_UK_Minds\Tollon Components\HostMonitor\Tollon.HostMonitor.TestGenerator\Program.cs:line 46
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
这是怎么回事?
Why is this happening?
更新:
我抓住从GitHub上最新的源代码,但现在这打破了由T4模板生成的code。生成的 ActiveRecord.cs
将无法编译,并给出了以下生成错误:
I grabbed the latest source from GitHub but this now breaks the code generated by the T4 template. The generated ActiveRecord.cs
won't compile and gives the following build error:
No overload for method 'Load' takes '1' arguments
[Path snipped]\subsonic-SubSonic-3.0-4748517\SubSonic.Tests\BugReports\Generated\ActiveRecord.cs
Line: 708
在code其中这个错误发生是这样的:
The code where this error is happening looks like:
public void Load(IDataReader rdr) {
Load(rdr, true);
}
public void Load(IDataReader rdr, bool closeReader) {
if (rdr.Read()) {
try {
rdr.Load(this); // <<-- Compile error happens here
SetIsNew(false);
SetIsLoaded(true);
} catch {
SetIsLoaded(false);
throw;
}
}else{
SetIsLoaded(false);
}
if (closeReader)
rdr.Dispose();
}
我也尝试无论是原3.0.0.3 ActiveRecord的模板,然后从 SubSonic.Tests
项目中的T4模板。
I did try both the original 3.0.0.3 ActiveRecord template and the T4 templates from the SubSonic.Tests
project.
推荐答案
我设法得到了一些时间来重温这一点,并解决的问题。
I managed to get a few moments to revisit this and solve.
这是扔我了,我错过了一个事实,即 rdr.Load()
方法是一个扩展方法(它的已的〜事情凌晨4点),并从已经有它的方法签名改成:
The thing that threw me was that I missed the fact that the rdr.Load()
method is an extension method (it was ~4am) and had had its method signature changed from:
public static void Load<T>(this IDataReader rdr, T item)
到
public static void Load<T>(this IDataReader rdr, T item, List<string> ColumnNames)
反正削减长话短说,一旦我意识到这一点,并已经做了查找用法上的方法,所有的电话都只是路过空
。然后,我只是修改了 ActiveRecord.tt
T4模板来反映这一点。
Anyway to cut a long story short, once I'd realised this and having done a find usages on the method, all of the calls were just passing null
. I then just modified the ActiveRecord.tt
T4 template to reflect this.
这篇关于为什么更新记录时亚音速和ActiveRecord的抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!