与小巧玲珑的插入时的NullReferenceException [英] NullReferenceException when inserting with Dapper

查看:126
本文介绍了与小巧玲珑的插入时的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码,我得到一个NullReferenceException说,对象引用未设置为对象的实例。我已经用短小精悍不太复杂的对象,但相同的格式插入成功,所以我不知道我做错了。

When I run the following code I get a NullReferenceException saying that an object reference not set to an instance of the object. I've successfully inserted with dapper using less complex objects but the same format, so am not sure what I'm doing wrong.

public void Foo(IEnumerable<FogbugzCase> cases) 
{
    // using a singleton for the SqlConnection
    using (SqlConnection conn = CreateConnection())
    {
        foreach (FogbugzCase fogbugzCase in cases)
        {
            conn.Execute("INSERT INTO fogbugz.Cases(CaseId, Title, ProjectId, CategoryId, Root, MilestoneId, Priority, Status, EstimatedHours, ElapsedHours, AssignedTo, ResolvedBy, IsResolved, IsOpen, Opened, Resolved, Uri, ResolveUri, OutlineUri, SpecUri, ParentId, Backlog) VALUES(@BugId, @Title, @ProjectId, @CategoryId, @RootId, @MilestoneId, @Priority, @StatusId, @EstimatedHours, @ElapsedHours, @PersonAssignedToId, @PersonResolvedById, @IsResolved, @IsOpen, @Opened, @Resolved, @Uri, @ResolveUri, @OutlineUri, @Spec, @ParentId, @Backlog);", new {BugId = fogbugzCase.BugId, Title = fogbugzCase.Title, ProjectId = fogbugzCase.Project.Id, CategoryId = fogbugzCase.Category.Id, RootId = fogbugzCase.Root, MilestoneId = fogbugzCase.Milestone.Id, Priority = fogbugzCase.Priority, StatusId = fogbugzCase.Status.Id, EstimatedHours = fogbugzCase.EstimatedHours, ElapsedHours = fogbugzCase.ElapsedHours, PersonAssignedToId = fogbugzCase.PersonAssignedTo.Id, PersonResolvedById = fogbugzCase.PersonResolvedBy.Id, IsResolved = fogbugzCase.IsResolved, IsOpen = fogbugzCase.IsOpen, Opened = fogbugzCase.Opened, Resolved = fogbugzCase.Resolved, Uri = fogbugzCase.Uri, OutlineUri = fogbugzCase.OutlineUri, Spec = fogbugzCase.Spec, ParentId = fogbugzCase.ParentId, Backlog = fogbugzCase.Backlog});
        }
    }
}



我第一次尝试做简单仅在 fogbugzCase 传递,而不是匿名的对象,但造成约的CategoryId一个不同的异常的方法。

I first tried doing the simpler way of just passing in fogbugzCase instead of the anonymous object but resulted in a different exception about CategoryId.

任何人看我缺少的是什么?

Anyone see what I'm missing?

推荐答案

这听起来像一个属性是。拆分代码:

It sounds like one of the properties is null. Split the code:

var args =  new {BugId = fogbugzCase.BugId, Title = fogbugzCase.Title, ProjectId = fogbugzCase.Project.Id, CategoryId = fogbugzCase.Category.Id, RootId = fogbugzCase.Root, MilestoneId = fogbugzCase.Milestone.Id, Priority = fogbugzCase.Priority, StatusId = fogbugzCase.Status.Id, EstimatedHours = fogbugzCase.EstimatedHours, ElapsedHours = fogbugzCase.ElapsedHours, PersonAssignedToId = fogbugzCase.PersonAssignedTo.Id, PersonResolvedById = fogbugzCase.PersonResolvedBy.Id, IsResolved = fogbugzCase.IsResolved, IsOpen = fogbugzCase.IsOpen, Opened = fogbugzCase.Opened, Resolved = fogbugzCase.Resolved, Uri = fogbugzCase.Uri, OutlineUri = fogbugzCase.OutlineUri, Spec = fogbugzCase.Spec, ParentId = fogbugzCase.ParentId, Backlog = fogbugzCase.Backlog});
conn.Execute("INSERT INTO fogbugz.Cases(CaseId, Title, ProjectId, CategoryId, Root, MilestoneId, Priority, Status, EstimatedHours, ElapsedHours, AssignedTo, ResolvedBy, IsResolved, IsOpen, Opened, Resolved, Uri, ResolveUri, OutlineUri, SpecUri, ParentId, Backlog) VALUES(@BugId, @Title, @ProjectId, @CategoryId, @RootId, @MilestoneId, @Priority, @StatusId, @EstimatedHours, @ElapsedHours, @PersonAssignedToId, @PersonResolvedById, @IsResolved, @IsOpen, @Opened, @Resolved, @Uri, @ResolveUri, @OutlineUri, @Spec, @ParentId, @Backlog);", args);



它的代码在第一行失败,它不是任何与短小精悍的 - 它没'吨甚至不能接近短小精悍的任何地方。你需要检查所有嵌套成员。

It the code fails on the first line, it isn't anything to do with dapper - it didn't even get anywhere near dapper. You'll need to check all the nested members.

这篇关于与小巧玲珑的插入时的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆