使用Dapper插入时出现NotSupportedException

时间:2013-06-28 23:35:10

标签: c# sql-server dapper

我正在尝试将一堆fogbugz案例插入到我自己的数据库中但得到NotSupportedException:System.Uri类型的成员OutlineUri不能用作参数值。

通过在传递args之前创建args,我能够确保我没有引用空对象(来自this 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);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

事实上,没有内置的Uri处理。只需使用您选择的Uri到字符串表示(有几种,它们的行为不同) - 例如:

...
OutlineUri = fogbugzCase.OutlineUri.OriginalString
...

这可能是我们可以自动完成的事情 - 但它根本就不是一个请求。