【问题标题】:Unable to determine if {entity} with assigned identifier {id} is transient or detached无法确定具有分配标识符 {id} 的 {entity} 是暂时的还是分离的
【发布时间】:2014-04-18 14:52:30
【问题描述】:

插入 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 映射:

public class UserProjectMap : ClassMap<UserProject>
{
    public UserProjectMap()
    {
        CompositeId()
            .KeyReference( x => x.User, "UserId" )
            .KeyReference( x => x.Project, "ProjectId" );
        Map( x => x.AccessLevel ).CustomType<Int32>();
    }
}

【问题讨论】:

    标签: c# sql nhibernate fluent-nhibernate


    【解决方案1】:

    将版本正确添加到您的模型。那将解决它。如果您认为您获得了多个版本的对象,请使用合并来水合对象并执行更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2019-09-16
      • 2019-10-25
      • 2018-06-29
      相关资源
      最近更新 更多