Is there a way to avoid the explicit Id
mapping in Fluent NHibernate ?
I want it to somehow generate the entry id automatically so that I wouldn't have to introduce it as a part of the class.
public class HeyMapping
{
public String Name { get; set; }
public DateTime Timestamp { get; set; }
}
public class HeyMapping : ClassMap<HeyMapping>
{
public HeyMapping()
{
Not.LazyLoad();
// I'm not particularly sure how this line works, but
// it fails the mapping unit test.
CompositeId().KeyProperty(x => x.Name);
Map(x => x.Name).Not.Nullable().Length(64);
Map(x => x.Timestamp).Not.Nullable();
}
}
If you want to have no id in your entity, you still have to map an Id so NHibernate knows the database column to use.
You can call
Id<TColumnDataType>("column_name");
Please note that you will give up some NHibernate functionality (specifically cascading updates and the ability to call SaveOrUpdate()
) and incur a performance penalty on the database side for having database-only identity (I believe NHibernate will have to make extra queries for comparison).
I usually concede this point and allow the Id
as the one persistence concern in my domain classes, so I would do this:
public class HeyMapping
{
protected internal int Id { get; set; } // persistence concern
public virtual String Name { get; set; }
public virtual DateTime Timestamp { get; set; }
}
I realize you might not want to do this; I'm just letting you know that there is a tradeoff.
创建一个基类,所有映射的实体都将从该基类继承,然后将Id属性添加到基类。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.