working with Fluent NHibernate and guid ids

2019-04-29 18:41发布

问题:

We're working with Fluent NHibernate 1.2 and our primary key is a guid saved in a nvarchar(32) column, working with Oracle 11gr2.

How can we make this work? (making an automatic conversion...)

Thanks ahead, random programmer...

UPDATE: forgot to mention, the guid is saved WITHOUT dashes ...

回答1:

Update:

You will have to implement your own IUserType to handle the dashless Guids.
You can read about it here:
http://dotnet.dzone.com/articles/understanding-nhibernate-type

The detail below is now irrelevant to the question but I'll keep it here for future reference for people to find.

Using Guids "normally"

In your entity the Id should be of type Guid:

public virtual Guid Id { get; private set; }

And in your ClassMap you should map it like this:

Id(x => x.Id)
  .Column("Id")
  .GeneratedBy.GuidComb();

This will use the recommended comb algorithm to generate new guids.

or

Id(x => x.Id)
  .Column("Id")
  .GeneratedBy.Guid();

to genertae new Guids using System.Guid

or

Id(x => x.Id)
  .Column("Id")
  .GeneratedBy.GuidNative();

if you want to let the database generate the Guid for you.