为什么SubSonic和ActiveRecord在更新记录时会抛出异常?

时间:2010-01-21 03:25:11

标签: 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,
    [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]

我正在使用SubSonic ActiveRecord 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();
    }

我确实尝试了原始的3.0.0.3 ActiveRecord模板和SubSonic.Tests项目中的T4模板。

1 个答案:

答案 0 :(得分:1)

我设法花了一些时间重新审视并解决。

让我感到震惊的是,我错过了rdr.Load()方法是一种扩展方法( 〜凌晨4点)并且其方法签名已更改为:

public static void Load<T>(this IDataReader rdr, T item)

到:

public static void Load<T>(this IDataReader rdr, T item, List<string> ColumnNames)

无论如何要简短地说,一旦我意识到这一点,并且已经对该方法进行了查找,所有的调用都只是通过了null。然后我修改了ActiveRecord.tt T4模板以反映这一点。