C# - Fluent NHibernate 映射问题
有没有办法避免 Fluent NHibernate 中的显式 Id
映射?
我希望它以某种方式自动生成条目 ID,这样我就不必将它作为类的一部分引入。
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望实体中没有 id,您仍然需要映射一个 Id,以便 NHibernate 知道要使用的数据库列。
您可以调用
请注意,您将放弃一些 NHibernate 功能(特别是级联更新和调用 SaveOrUpdate() 的能力),并因仅拥有数据库身份而在数据库端产生性能损失(我相信NHibernate将不得不进行额外的查询来进行比较)。
我通常承认这一点,并允许将
Id
作为我的域类中的一个持久性关注点,因此我会这样做:我意识到您可能不想这样做;我只是让你知道这是一个权衡。
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
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:I realize you might not want to do this; I'm just letting you know that there is a tradeoff.
创建一个所有映射实体都继承自的基类,然后将 Id 属性添加到您的基类。
Create a base class from which all of your mapped entities inherit, then add an Id property to your base class.