简体   繁体   中英

NHibernate commits one child instead of two

I have again some problems with NHibernate. I want to add two child objects to a parent at the same time. The problem is that only the first child gets committed and the second child gets ignored. I don't even get an error message for the second child.

public void CreateKeyword(CreateKeywordRequest request)
{
    Collection collection = _collectionRepository.FindCollection(request.IdentityToken, request.CollectionName);

    collection.AddKeyword(request.CategoryName, request.KeywordName, request.KeywordDescription);

    _uow.Commit();
}

-

public void AddKeyword(string categoryName, string keywordName, string keywordDescription)
{

    Category category = Categories.Where(c => c.CategoryName == categoryName).FirstOrDefault();

    Keyword keyword = new Keyword { Category = category, KeywordName = keywordName, Description = keywordDescription };

    category.Keywords.Add(keyword);

    Test testOne= new Test{ Keyword = keyword, name = "test1" };
    Test testTwo= new Test{ Keyword = keyword, name = "test2" };

    //Only testOne gets committed and testTwo gets ignored
    keyword.Tests.Add(testOne);
    keyword.Tests.Add(testTwo);

    ThrowExceptionIfKeywordIsInvalid(keyword);
}

我怀疑Tests是某种Set(例如ISet),并且Equals测试方法仅在Keyword属性上进行比较

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