nhibernate Iesi ISet无法删除() [英] nhibernate Iesi ISet fails to Remove()

查看:125
本文介绍了nhibernate Iesi ISet无法删除()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个由NHibernate处理的类:AssetGroup,Asset AssetGroup具有ISet _assets集合. AssetGroup的构造函数会说

I have 2 class'es that are handled by NHibernate : AssetGroup , Asset The AssetGroup has a ISet _assets collection. The constructor of AssetGroup will say

_assets = new HashSet<Asset>();

我有一些操作要添加,删除AssetGroup中的资产

I have some operation to add , remove asset in AssetGroup

    public abstract class Entity<Tid>
{
    public virtual Tid Id { get; protected set; }

    public override bool Equals(object obj)
    {
        return Equals(obj as Entity<Tid>);
    }

    public static bool IsTransient(Entity<Tid> obj)
    {
        return obj != null && Equals(obj.Id, default(Tid));
    }

    private Type GetUnproxiedType()
    {
        return GetType();
    }

    public virtual bool Equals(Entity<Tid> other)
    {
        if (other == null)
            return false;
        if (ReferenceEquals(this, other))
            return true;
        if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id))
        {
            var otherType = other.GetUnproxiedType();
            var thisType = GetUnproxiedType();
            return thisType.IsAssignableFrom(otherType) || otherType.IsAssignableFrom(thisType);
        }
        return false;
    }

    public override int GetHashCode()
    {
        if (Equals(Id, default(Tid)))
        {
            return base.GetHashCode();
        }
        else
        {
            return Id.GetHashCode();
        }
    }
}

///////////////////////////////////////


public class AssetGroup : Entity<int>
{
    public AssetGroup()
    {
        this._assets = new HashedSet<Asset>();
    }
    virtual public Guid SecurityKey {get; set;}

    virtual public string Name { get; set; }

    private ISet<Asset> _assets;
    virtual public ISet<Asset> Assets
    {
        get { return _assets; }
        protected set { _assets = value; }
    }

    virtual public bool AddAsset(Asset asset)
    {
        if (asset != null && _assets.Add(asset))
        {
            return true;
        }
        return false;
    }

    virtual public bool RemoveAsset(Asset asset)
    {
        Asset target = null;
        foreach (var a in _assets)
        {
            var x = a.GetHashCode();
            var b = a.Equals(asset);
            if (a.Equals(asset))
                target = a;
        }
        if (target == null)
            return false;
        if (asset != null && _assets.Remove(target))
        {
            return true;
        }
        return false;
    }

}

////////////////////////////////////////

public class Asset : Entity<int> 
{
    public Asset()
    {
        SecurityKey = Guid.NewGuid();
    }

    public virtual Guid SecurityKey { get; set; }

    virtual public int AssetGroupID { get { return (AssetGroup != null ? AssetGroup.Id : 0); } }

    virtual public string Name { get; set; }

    virtual public AssetGroup AssetGroup { get; set;}

    virtual public void SetAssetGroup(AssetGroup assetGroup)
    {
        AssetGroup prevRef = AssetGroup;
        if (prevRef == assetGroup)
            return;
        AssetGroup = assetGroup;
        if (prevRef != null)
            prevRef.Assets.Remove(this);
        if (assetGroup != null)
            assetGroup.Assets.Add(this);
    }
}

RemoveAsset无法删除资产.我有一个foreach来检查资产是否存在于_assets中.我放置了断点来跟踪它,并且foreach循环可以找到要删除的资产(目标).奇怪的是,当我要求_assets删除目标时. 它无法删除并返回false. 另外,如果我问_assets.Contains(target)..即使RemoveAsset中的foreach循环可以找到目标,它也会返回false ..

The RemoveAsset fails to remove asset . I have a foreach to check if the asset exists in the _assets . I put breakpoints to trace thru it and foreach loop can find the asset (targe) to be RemoveAsset'ed . Strangely enough , when I ask _assets to Remove the target. It fails to remove and return false. Also if I asks _assets.Contains(target) .. it also return false .. even if the foreach loop in the RemoveAsset can find the target...

两个休眠映射是

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="MySystem.Domain"
               namespace="MySystem.Domain" auto-import="true">

有人可以帮助我吗?

推荐答案

我认为我也遇到了同样的问题,问题"是我将该项目添加到了HashedSet中,之后 进行了更改(分配ID),更改GetHashCode的结果.

I think I just faced the same issue, and the "problem" was that I added the item to the HashedSet and afterwards changed it (to assign IDs), changing the result of the GetHashCode.

我认为这意味着对象最终出现在内部字典的另一个存储桶中,因此包含返回的false.

I think this means that the object ended up in a different bucket in the inner dictionary, hence Contains returning false.

这篇关于nhibernate Iesi ISet无法删除()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆