Bugs When Remove Items With The HasAndBelongsToMany Type

Topics: Developer Forum
Sep 9, 2008 at 12:05 PM

    public abstract class TableC : LinqObjectModel<TableC>
        public abstract string Title { get; set; }

        [HasAndBelongsToMany(OrderBy = "Id")]
        public abstract IList<TableD> TD { get; set; }

    public abstract class TableD : LinqObjectModel<TableD>
        public abstract string Name { get; set; }

        [HasAndBelongsToMany(OrderBy = "Id")]
        public abstract IList<TableC> TC { get; set; }


                 DbContext de = DbEntry.Context;
                               de.Create_ManyToManyMediTable(typeof(TableC), typeof(TableD));

                                var t1 = TableC.New();
                                t1.Title = "Article1";

                                var t3 = TableD.New();
                                t3.Name = "Tag1";

                                var t2 = TableC.FindOne(p => p.Id == 1);

                //Begin Remove
                                var t4=TableC.FindById(1);
                                var count = t4.TD.Count;
                                var t5 = TableD.FindById(1);
                                var count2 = t5.TC.Count;

                                bool b = t4.TD.Remove(t5);

//here b= false and can't trace the delete sql

                                bool b2 = t5.TC.Remove(t4);

//here b2= false and can't trace the delete sql

Sep 9, 2008 at 11:28 PM
bool b = t4.TD.Remove(t5);
bool b2 = t5.TC.Remove(t4);
are wrong.

The remove function only remove the object it contains. It's different with the object just read by FindById.
They may have different values.
Do you think I should throw an exception if it's not in the List or just allow remove the same Id item?
Sep 10, 2008 at 4:38 AM
Edited Sep 10, 2008 at 4:42 AM
I don't understand what you mean with the last sentence.

I am very care about that how can I delete one specific record in the cross table?

我怎么才能删掉HasAndBelongsToMany 里面特定的一条数据呢?如果用remove 命令不行的话,应该用什么命令呢?
findbyId只能找到母表的数据,不能locate cross table的数据。
Sep 10, 2008 at 10:22 AM
I think about it for a while and I feel maybe you are right. I should allow remove the same Id item from the List.

我记得 DbEntry 中 IList.Remove 的实现是根据对象比较的,因为 IList 中的对象,和 FindById 的对象虽然值相同,却是不同的对象,所以不会被移除。
现在可以通过枚举 IList 的方式,找到正确的对象,调用 Remove 或者 RemoveAt 来移除。
不过我现在也觉得,应该让 Remove 判断只要对象的 Id 相同,则进行移除。
Sep 10, 2008 at 1:46 PM
ok, 按照这种思路修改了。
而且,因为只判断 Id,所以应该下面的 Remove 方式也可行(TableD 要有 Init 函数):
t4.TD.Remove(TableD.New().Init(1, null));
Sep 10, 2008 at 3:12 PM
OK,I got it, I will try it thanks.$0$0$0$0$0$0$0哦,我明白你的意思了。$0$0$0$0$0你原来的设计是正确的!$0$0$0$0$0只是我没意识到你是这么设计的!$0$0$0$0$0thanks very much$0