插入UserProject实体时,我收到以下警告。数据插入正确我只是试图摆脱我所有的nHibernate错误,如果可能的话。我正在使用SaveOrUpdate,我想避免使用特定的Save()或Update(),如果摆脱这个错误,将非常感谢帮助。
nHibernate Profiler中的错误:
WARN:
Unable to determine if IdeBoss.DataAccess.Entities.UserProject with assigned identifier IdeBoss.DataAccess.Entities.UserProject is transient or detached; querying the database. Use explicit Save() or Update() in session to prevent this.
我的实体:
public class UserProject
{
public virtual User User { get; set; }
public virtual Project Project { get; set; }
public virtual AccessLevel AccessLevel { get; set; }
public override bool Equals( object obj )
{
if ( ReferenceEquals( null, obj ) )
return false;
if ( ReferenceEquals( this, obj ) )
return true;
if ( obj.GetType() != this.GetType() )
return false;
return Equals( (UserProject)obj );
}
private bool Equals( UserProject other )
{
return Equals( User.Id, other.User.Id ) && Equals( Project.Id, other.Project.Id );
}
public override int GetHashCode()
{
unchecked
{
return ( ( User != null ? User.Id : 0 ) * 397 ) ^ ( Project != null ? Project.Id : 0 );
}
}
}
nHibernate Map:
public class UserProjectMap : ClassMap<UserProject>
{
public UserProjectMap()
{
CompositeId()
.KeyReference( x => x.User, "UserId" )
.KeyReference( x => x.Project, "ProjectId" );
Map( x => x.AccessLevel ).CustomType<Int32>();
}
}
答案 0 :(得分:1)
向您的模型添加版本属性。这将解决它。如果您认为您正在获得该对象的多个版本,请使用merge对该对象进行水合并执行更新。