Is this bug?

Topics: Developer Forum, User Forum
Oct 14, 2009 at 3:31 AM

When I removed an item from the HasMany<T> list and then called Save(), it didn't delete the record from the DB. Then I found line 507~516 in the DbContext.cs as below:

                                    if (f.IsHasMany)
                                    {
                                        var ho2 = (IHasMany)ho;
                                        foreach (object item in ho2.RemovedValues)
                                        {
                                            Save(item);
                                        }
                                    }

                                    CommonHelper.TryEnumerate(llo, e2);

Is it a bug? Should the line 512 "Save(item)" be corrected to "Delete(item)"? Thank you!

当我从HasMany<T>移除一行并保存父对象的时候,数据并没有删除,查源代码发现DbContext.cs的512行有疑问,能否解释一下这是不是bug?谢谢!

Coordinator
Oct 14, 2009 at 8:32 AM

It's not a bug.

It just remove the relation of them when you call HasMany<T>.Remove. The object need to delete manually.

不是bug。Remove只移除两个对象的关系,对象本身需要手动删除。

Oct 14, 2009 at 10:19 AM

Thanks for reply!

还有一个疑问,如果是只移除两个对象的关系,为什么Save后重新读取数据,两个对象的关系还在?

我调试时发现Save(item)中的item的外键值已经更新,但是它的m_UpdateColumns是没有值的,所以SmartUpdate的时候没有更新到。

再往深处看,当我做HasMany<T>的Remove的时候,背后有以下代码:

protected override void OnRemoveItem(T item)
        {
            ObjectInfo oi = ObjectInfo.GetInstance(typeof(T));
            if (!oi.IsNewObject(item))
            {
                Type ot = owner.GetType();
                MemberHandler mh = oi.GetBelongsTo(ot);
                IBelongsTo o = mh.GetValue(item) as IBelongsTo;
                o.ForeignKey = CommonHelper.GetEmptyValue(o.ForeignKey.GetType());
                _RemovedValues.Add(item);
            }
        }

好像o.ForeignKey的更新并没有反映到m_UpdateColumns中,我看得有点晕,请老大再解释一下,谢谢!

Coordinator
Oct 14, 2009 at 11:26 AM

没有更新 m_UpdateColumns 是一个 bug,谢谢。

Coordinator
Oct 14, 2009 at 11:26 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.