如何使用nhibernate createcriteria获取实体的id
新手警报 我正在尝试检查数据库中是否存在实体,如果存在,我想更新它,否则创建一个新实体。但是 CreateCriteria 使用总是返回一个没有 id 的实体?有什么想法吗?我使用 Fluent nhibernate 进行手动映射,即使用 ClassMap;
基类-仅持有 Id 公共属性
公共抽象类EntityBase:IEquatable { 公共虚拟int Id { 得到;放; }
public virtual bool Equals(EntityBase obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (GetType() != obj.GetType()) return false;
return obj.Id == Id;
}
}
映射;
public class ProjectNameMap: ClassMap
; { 公共项目名称映射() { 表(“dbo.TABLENAME”); Id(x => x.Id).GenerateBy.Identity(); Map(x => x.PROJECT_NAME).Not.Nullable(); 地图(x => x.PROJECT_DESCRIPTION); } }
取回实体;
public static T GetEntityByRestrictions
(String propertyName, String propertyValue) 其中 T:EntityBase { 使用 (var session = SessionManager.CreateSessionFactory().OpenSession()) { 使用 (var transaction = session.BeginTransaction()) { var实体= session.CreateCriteria (propertyValue) .Add(限制.Eq(属性名称, 属性值)) .UniqueResult (); 返回实体; } } } }
我尝试了两个愚蠢的步骤(别笑) 1.我尝试的愚蠢步骤是手动设置 Id =52 匹配现有数据库条目,并且我不断收到项目名称的主键违规,因为数据库需要唯一的项目名称。
- 更多愚蠢的步骤(我能听到笑声)修改了映射文件以包含 Map(x=>x.Id).Update().Insert() ,这导致 INSERT_IDENTITY 设置为 OFF(或其他)。
那么获取具有 Id 的实体并随后更新的最佳方法是什么,我的 CreateCriteria 有问题吗?
Newbie Alert
I am trying to check if an entity exists in the database, if it does i want to update it else create a new entity. But CreateCriteria use always returns an entity with no id? Any ideas why? I am using fluent nhibernate for manual mapping i.e use of ClassMap;
Base class -hold only the Id public property
public abstract class EntityBase : IEquatable
{
public virtual int Id { get; set; }
public virtual bool Equals(EntityBase obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (GetType() != obj.GetType()) return false;
return obj.Id == Id;
}
}
MAPPING;
public class ProjectNameMap: ClassMap<ProjectName> { public ProjectNameMap() { Table("dbo.TABLENAME"); Id(x => x.Id).GeneratedBy.Identity(); Map(x => x.PROJECT_NAME).Not.Nullable(); Map(x => x.PROJECT_DESCRIPTION); } }
Getting back the entity;
public static T GetEntityByRestrictions<T>(String propertyName, String propertyValue) where T:EntityBase { using (var session = SessionManager.CreateSessionFactory().OpenSession()) { using (var transaction = session.BeginTransaction()) { var entity= session.CreateCriteria<T>(propertyValue) .Add(Restrictions.Eq(propertyName, propertyValue)) .UniqueResult<T>(); return entity; } } } }
Two silly steps i tried (dont laugh)
1. Silly step i tried was to manually set the Id =52 matching existing database entry and i keep on getting a primary key violation on project name as the database expects unique project name.
- More silly steps (i can hear laughter) modified mapping file to include Map(x=>x.Id).Update().Insert() and this lead to INSERT_IDENTITY set to OFF (or somethin).
So whats the best way to get an entity with Id and update afterwards,is there something wrong with my CreateCriteria?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信在会话中调用 Merge 方法将完全满足您的要求。
只需将要更新/插入的实体作为参数放入,如果该实体已存在,它将更新属性,如果不存在,它将保留新实例。这样您就不需要CreateCriteria,并且您的解决方案将会简单得多。
I believe calling the Merge method on the session will do exactly what you want.
Just put in the entity you want to update/insert as an argument and it will update the properties if the entity already exists or persist the new instance if it does not. This way you don't need the CreateCriteria and your solution will be a lot simpler.