使用Dapper插入时出现NotSupportedExcepti

使用Dapper插入时出现NotSupportedExcepti

本文介绍了使用Dapper插入时出现NotSupportedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to insert a bunch of fogbugz cases into my own database but get a NotSupportedException: The member OutlineUri of type System.Uri cannot be used as a parameter value.

通过在传递参数之前创建args,我可以确保我没有引用空对象(来自 SO问题)。我看到抛出异常时,fogbugzCase.OutlineUri具有有效值。

By creating args before passing it in I was able to ensure that I'm not referencing a null object (from this SO question). I can see that fogbugzCase.OutlineUri has a valid value when the exception is thrown.

有什么想法吗?

以下是代码(对许多缩进表示歉意):

Here's the code (apologies about lots of indentation):

    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-to-string表示形式(有多种表示形式,它们的行为就不同)-例如:

Indeed, there is no inbuilt handling of Uri. Just use your chosen Uri-to-string representation (there are several and they behave differently) - for example:

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

也许我们可以自动执行-但只是从来没有将它作为请求。

This is something that perhaps we could do automatically - but simply it has never come up as a request.

这篇关于使用Dapper插入时出现NotSupportedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:08