English 中文(简体)
全部删除 Nhhiberate 的孤儿
原标题:All delete orphan for Nhibernate

因此,我有一个名为“秩序”的母体, 以及一个叫做“秩序”的子体的集合, 叫做“线粒子”的子体, 与卡丝卡德一起, 设置在新赫伯纳的全世无主的孤儿身上。这是我的代码。

using( var session = _sessionManager.GetSession())
        using (var transaction = session.BeginTransaction())
        {

            var order = _repository.GetOrderEagerlyByOrderId(session, fromDb.Id);
            var now = DateTime.Now;
            const string user = "GNB\Username";
            var future = now.AddYears(1);
            var taxType = new TaxType(0, "Code", "AlternateNameE", "AlternateNameF", "NameE", "NameF", "DescriptionE", "DescriptionF", 13, now, future, user, now, user, now);
            var _serviceCatRep = new ServiceCatalogueRepository();

            var serviceCatalogueItem = _serviceCatRep.GetServiceCatalogueItemByCode(session, "VR-PASS");
            var orderLineItem1 = new OrderLineItem(0, null, "DescriptionE", "DescriptionF", 1, 10, null, null, 5, false, serviceCatalogueItem, null, user, now, user, future);

            order.OrderLineItems.Clear();

            order.OrderLineItems = order.OrderLineItems == null ? new List<IOrderLineItem> { orderLineItem1 } : new List<IOrderLineItem>(order.OrderLineItems) { orderLineItem1 };

            _repository.SaveOrUpdate(session, order);
            transaction.Commit();
        }

transaction.communit (); 上失败,错误如下:

Nhembernate.HebertateException: 一家拥有级联的收藏 = “全电子孤儿” 不再被拥有实体实例所引用。

举个例子说,所有孤儿和所有孤儿之间有什么区别。

最佳回答

你有:

order.OrderLineItems.Clear();

order.OrderLineItems = order.OrderLineItems == null 
    ? new List<IOrderLineItem> { orderLineItem1 } 
    : new List<IOrderLineItem>(order.OrderLineItems) { orderLineItem1 };

首先,如果顺序。 OrderLineemists 是无效的, 那么顺序就是无效的。 OrderLineemists. Clear () 将会推出一个 NullReference Reference Exception. Clear () 。 其次, 问题出现的原因是您重新指定了要排序的新列表 。 OrderLineemts so Nebertnate 不知道如何将删除内容升级 。 要让它生效, 只需将第二行修改为 :

order.OrderLineItems.Add(orderLineItem1);
问题回答

在映射中 - 将收藏标为反向 ()

HasMany(x=>x.OrderLineItems)
   .Inverse(); 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签