Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

working with Fluent NHibernate and guid ids

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 ...

like image 485
Random programmer Avatar asked Jun 20 '11 08:06

Random programmer


People also ask

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.


1 Answers

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.

like image 95
Variant Avatar answered Sep 22 '22 10:09

Variant