使用Dapper插入时出现NotSupportedException
本文关键字:NotSupportedException Dapper 插入 使用 | 更新日期: 2023-09-27 18:20:16
我试图在自己的数据库中插入一堆fogbugz案例,但得到了NotSupportedException:System.Uri类型的成员OutlineUri不能用作参数值。
通过在传入之前创建args,我能够确保我没有引用null对象(来自这个SO问题)。我可以看到,当抛出异常时,fogbugzCase.OutlineUri有一个有效的值。
有什么想法吗?
以下是代码(对大量缩进表示歉意):
public void Foo(IEnumerable<FogbugzCase> cases)
{
using (SqlConnection conn = CreateConnection())
{
TruncateWorkingTable(conn, "Cases");
foreach (FogbugzCase fogbugzCase in cases)
{
int categoryId = fogbugzCase.Category.Id;
int? assigneeId = null;
if (fogbugzCase.PersonAssignedTo != null)
assigneeId = fogbugzCase.PersonAssignedTo.Id;
int? resolveeId = null;
if (fogbugzCase.PersonResolvedBy != null)
resolveeId = fogbugzCase.PersonResolvedBy.Id;
var args = new
{
BugId = fogbugzCase.BugId,
Title = fogbugzCase.Title,
ProjectId = fogbugzCase.Project.Id,
CategoryId = categoryId,
RootId = fogbugzCase.Root,
MilestoneId = fogbugzCase.Milestone.Id,
Priority = fogbugzCase.Priority,
StatusId = fogbugzCase.Status.Id,
EstimatedHours = fogbugzCase.EstimatedHours,
ElapsedHours = fogbugzCase.ElapsedHours,
PersonAssignedToId = assigneeId,
PersonResolvedById = resolveeId,
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);
}
}
}
实际上,没有对Uri的内置处理。只需使用您选择的Uri来字符串表示(有几种,它们的行为不同)-例如:
...
OutlineUri = fogbugzCase.OutlineUri.OriginalString
...
这也许是我们可以自动完成的事情,但简单地说,它从来都不是一个请求。