Proper Guid Mapping. 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; 开发者_如何学JAVAset; }
public DateTime Timestamp { get; set; }
}
How do I properly map Post
class using xml-mapping? I'm asking about Id.
<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.
I'm using Asp.Net Core, with the Oracle.ManagedDataAccess NuGet package, and FluentNhibernate as mapping strategy.
In my case it was giving some errors when trying to get the data. The errors stopped, after mapping the Guid column as follows:
Id(x => x.Id, x =>
{
x.Generator(Generators.Guid);
x.Type(NHibernateUtil.Guid);
x.Column("ID");
});
精彩评论