C# 为什么亚音速和ActiveRecord在更新记录时引发异常?
我在MS SQL Server 2008标准版中有下表:C# 为什么亚音速和ActiveRecord在更新记录时引发异常?,c#,activerecord,subsonic,subsonic3,C#,Activerecord,Subsonic,Subsonic3,我在MS SQL Server 2008标准版中有下表: 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, [Meth
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]
我使用的是T4模板,代码如下:
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();
}
引发的异常是:
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:
为什么会这样
更新:
我从GitHub获取了最新的源代码,但这现在破坏了T4模板生成的代码。生成的ActiveRecord.cs
将无法编译,并出现以下生成错误:
No overload for method 'Load' takes '1' arguments
[Path snipped]\subsonic-SubSonic-3.0-4748517\SubSonic.Tests\BugReports\Generated\ActiveRecord.cs
Line: 708
发生此错误的代码如下所示:
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();
}
公共空隙荷载(IDataReader rdr){
载荷(rdr,真);
}
公共无效荷载(IDataReader rdr、bool closeReader){
if(rdr.Read()){
试一试{
加载(这个);//我设法抽出一些时间来重新讨论这个问题并解决它
让我吃惊的是,我忽略了rdr.Load()
方法是一个扩展方法(它是~4am),并且它的方法签名已从:
public static void Load<T>(this IDataReader rdr, T item)
公共静态空荷载(此IDataReader rdr,T项)
致:
公共静态无效负载(此IDataReader rdr,T项,列表列名)
总之,长话短说,一旦我意识到这一点,并对该方法进行了一次用法查找,所有调用都只是传递null
。然后我修改了ActiveRecord.tt
T4模板以反映这一点。只是为了澄清这里发生了什么。亚音速repo的最后一次签入是修复Linq投影,这样做了,但它更改了扩展方法的方法签名,并且该更改需要向下游传播到t4模板。正如您在回答中所说的,任何人都可以通过确保其模板传递一个默认值为null的额外参数来解决此问题,但它需要修复,因此我将尝试找时间帮助解决此问题尽快。@Adam-我不介意在周末解决这个问题,另外我还做了一些Git练习,如果能对项目做一点小改动就好了。那太好了,去做吧。
public static void Load<T>(this IDataReader rdr, T item, List<string> ColumnNames)