Dapper

2013-06-28 18 views
5

ile eklerken NullReferenceException Aşağıdaki kodu çalıştırdığımda, bir nesne başvurusunun nesnenin örneğine ayarlanmadığını söyleyen bir NullReferenceException alıyorum. Ben daha az karmaşık nesneleri kullanarak aynı formatta dapper ile başarıyla ekledim, bu yüzden yanlış yaptığımdan emin değilim.Dapper

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

Ben ilk sadece yerine anonim nesnenin fogbugzCase içinde geçen basit bir yol yapıyor çalıştı ama KategoriNo hakkında farklı bir istisna sonuçlandı.

Eksik olduğumu gören var mı?

+1

Bir kesme noktası koyun ve tüm nesnelerinizi kontrol edin. Tahmin etmem gerekirse, CategoryId hakkında söylediklerinize göre, "fogbugzCase.Category" boş olabilir. Ama her şeyi kontrol et. Boş bir referansın bir özelliğine erişirseniz, bir "NullReferenceException" alırsınız. – zimdanen

+0

@zimdanen thx, null bir şey vardı - sadece bunu kontrol etmek için bir yol bulmaya çalışıyorum. –

+1

NullReferenceException'ın neredeyse tüm örnekleri aynıdır. Bazı ipuçları için lütfen ".NET'te NullReferenceException nedir?" (Http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) konusuna bakın. Çalıştığı –

cevap

6

Özelliklerden biri null gibi görünüyor. kodunu Böl: Bu kod ilk satırda başarısız

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 bir ilgisi değil - hatta dapper yakın bir sonuç elde edemedik. Tüm iç içe geçmiş üyeleri kontrol etmeniz gerekir.

+0

, thx. Şimdi sadece [burada] hakkında sorduğum bir NotSupportedException alıyorum (http://stackoverflow.com/questions/17375134/notsupportedexception-when-inserting-with-dapper) –

İlgili konular