Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Guid Mapping. Nhibernate

Tags:

c#

nhibernate

I use an abstract Entity class which contains a Guid:

public abstract class Entity
{
    public /*virtual*/ Guid Id { get; set; }
}

Suppose I also have a class like:

public class Post : Entity
{
    public String Title { get; set; }
    public String Content { get; set; }
    public DateTime Timestamp { get; set; }
}

How do I properly map Post class using xml-mapping? I'm asking about Id.

like image 351
lexeme Avatar asked Sep 28 '11 18:09

lexeme


People also ask

What is NHibernate mapping?

nhibernate Mappings Xml Mappings It is a syntax xml file which contains the metadata required for the object/relational mapping. The metadata includes declaration of persistent classes and the mapping of properties (to columns and foreign key relationships to other entities) to database tables.

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

<id name="Id">
    <generator class="guid"/>
</id>

This will generate Guids on the client using the Guid.NewGuid() method.

Alternative generators are:

  • guid.native - generates the Guids on the server side, e.g. using NEWID() on SQL server
  • guid.comb - generates "sequential" Guids, which reduces index fragmentation.

I would recommend guid.comb for most applications that use Guid identifiers.

like image 115
Jonas Høgh Avatar answered Oct 06 '22 01:10

Jonas Høgh