Dapper로 삽입 할 때 NullReferenceException 발생

c# dapper sql-server

문제

다음 코드를 실행할 때 개체 참조가 개체의 인스턴스로 설정되지 않는다는 NullReferenceException이 발생합니다. 덜 복잡한 객체를 사용하지만 동일한 형식을 사용하여 성공적으로 삽입했습니다. 그래서 내가 잘못하고있는 것이 확실하지 않습니다.

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에 대해서는 다른 예외가 발생했습니다.

아무도 내가 놓친 걸 본다?

수락 된 답변

속성 중 하나가 null . 코드 분할 :

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

코드가 첫 줄에서 실패하면, 그것은 dapper와는 아무런 관련이 없습니다. 중첩 된 멤버를 모두 확인해야합니다.



아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow