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

发布于 2024-08-18 05:43:57 字数 3265 浏览 7 评论 0原文

我在 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 项目。

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]

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?

Update:

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

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();
    }

I did try both the original 3.0.0.3 ActiveRecord template and the T4 templates from the SubSonic.Tests project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-08-25 05:43:57

我设法抽出一些时间来重新审视这个问题并解决这个问题。

让我困惑的是,我错过了 rdr.Load() 方法是一个扩展方法(它 ~4am)这一事实,并且它的方法签名已更改from:

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

to:

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

无论如何,长话短说,一旦我意识到这一点并对该方法进行了查找用法,所有调用都只是传递 null。然后,我修改了 ActiveRecord.tt T4 模板来反映这一点。

I managed to get a few moments to revisit this and solve.

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)

to:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文