需要 System.String,得到 System.Guid

发布于 2024-09-28 18:34:21 字数 1576 浏览 0 评论 0原文

我希望有一个非常简单的解释为什么我会收到此错误。

我正在使用 S#arpArcitecture 1.6。在 64 位 Windows 7 安装上。

以下代码的第 3 行给出错误:

{"Provided id of the error type. Expected: System.String, got System.Guid"} System.Exception {NHibernate.TypeMismatchException}

1 public Category GetCategory(Guid id)
2    {
3        Category cat = categoryRepository.Get(id);
4        return cat;
5    }

Supporting信息

表 (SQL Server 2008)

CREATE TABLE [dbo].[MasterCategories] (
    [masterCategoryId] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [organizationId] [nchar](5) NOT NULL,
    [categoryNumber] [nvarchar](25) NOT NULL
)

实体定义

public class Category : EntityWithTypedId<Guid>

流畅映射

public void Override(AutoMapping<Category> mapping)
    {
        mapping.Table("MasterCategories");

        mapping.Id(x => x.Id).Column("masterCategoryId");
        mapping.Map(x => x.Number).Column("categoryNumber");

        mapping.References(x => x.Organization)
            .Column("organizationId")
            .Cascade.All();
    }

存储库接口

public interface ICategoryRepository : IRepositoryWithTypedId<Category,Guid>
{
}

存储库< /强>

public class CategoryRepository : 
             RepositoryWithTypedId<Category,Guid>, 
             ICategoryRepository
{ }   

I am hoping there is a very simple explanation why I am getting this error.

I am using S#arpArcitecture 1.6. on a 64bit Windows 7 install.

Line 3 of the following code gives the error:

{"Provided id of the wrong type. Expected: System.String, got System.Guid"} System.Exception {NHibernate.TypeMismatchException}

1 public Category GetCategory(Guid id)
2    {
3        Category cat = categoryRepository.Get(id);
4        return cat;
5    }

Supporting Info

Table (SQL Server 2008)

CREATE TABLE [dbo].[MasterCategories] (
    [masterCategoryId] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [organizationId] [nchar](5) NOT NULL,
    [categoryNumber] [nvarchar](25) NOT NULL
)

Entity Definition

public class Category : EntityWithTypedId<Guid>

Fluent Mapping

public void Override(AutoMapping<Category> mapping)
    {
        mapping.Table("MasterCategories");

        mapping.Id(x => x.Id).Column("masterCategoryId");
        mapping.Map(x => x.Number).Column("categoryNumber");

        mapping.References(x => x.Organization)
            .Column("organizationId")
            .Cascade.All();
    }

Repository Interface

public interface ICategoryRepository : IRepositoryWithTypedId<Category,Guid>
{
}

Repository

public class CategoryRepository : 
             RepositoryWithTypedId<Category,Guid>, 
             ICategoryRepository
{ }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2024-10-05 18:34:22

我认为你的映射需要是这样的:

public void Override(AutoMapping<Category> mapping)
{
    mapping.Table("MasterCategories");
    mapping.Id(x => x.Id).Column("masterCategoryId").GeneratedBy.Guid();
    mapping.Map(x => x.Number).Column("categoryNumber");

    mapping.References(x => x.Organization)
            .Column("organizationId")
            .Cascade.All();
}

I think your mapping needs to be like this:

public void Override(AutoMapping<Category> mapping)
{
    mapping.Table("MasterCategories");
    mapping.Id(x => x.Id).Column("masterCategoryId").GeneratedBy.Guid();
    mapping.Map(x => x.Number).Column("categoryNumber");

    mapping.References(x => x.Organization)
            .Column("organizationId")
            .Cascade.All();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文