views:

81

answers:

1

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
{ }   
+1  A: 

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();
}
Chris Conway
Chris, thanks for the answer, but only changing generatedBy to Guid does not seem to affect how the type is passed to NHibernate. When I change the identity type to string in all the appropriate places, the Get command works.I am now working on saving which will need to have GenerateBy set.
Jason Watts
Chris, thanks. This turned out to be helpful.
Jason Watts
no prob! glad it helped.
Chris Conway