简体   繁体   中英

Unable to determine if {entity} with assigned identifier {id} is transient or detached

When inserting a UserProject entity I get the following warning. The data is inserted correctly I am just trying to get rid of all my nHibernate errors if possible. I am using SaveOrUpdate an I would like to avoid using specific Save() or Update() and help would be greatly apprectiated on how to get rid of this error.

Error in 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.

My Entity:

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>();
    }
}

Add Version Propertly to your model. That will fix it. If you think you are getting more then one version of the object, hydrate the object with merge and perform the update.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM