BelongsTo......

Mar 16, 2009 at 9:29 AM
Edited Mar 16, 2009 at 9:39 AM
you can ignore this thread, i have found solution....3ks...
..................................
long long long ago I have asked you about this question...

I just talk about this again, if you have any new idea to give id directly

now~,we have to do this
--------------------------------------------------------

public abstract class vtest : LinqObjectModel<vtest>
    {
        public abstract string qx { get; set; }
    }

    public abstract class vtest2 : LinqObjectModel<vtest2>
    {
        [BelongsTo, DbColumn("vtestId"), Index]
        public abstract vtest test { get; set; }
    }

public class t
    {
        public static void t1(){
            DbEntry.Context.DropAndCreate(typeof(vtest));
            DbEntry.Context.DropAndCreate(typeof(vtest2));

            var x=vtest.New();
            x.qx = "1";
            x.Save();

            var x3 = vtest.New();
            x3.qx = "1";
            x3.Save();

            var x1 = vtest2.New();
            x1.test = x;//如今我们只能给belongsto变量这样赋值
            x1.Save();

            var x2 = vtest2.FindById(1);
            x2.test = x3;//如今我们只能给belongsto变量这样赋值
            x2.Save();
        }
    }
-----------------------------------------------------------
is there any way that i can give Id directly?.................
if one class have lots of belongsTo,and they need to update again and again。
then we need select again and again then update....
-----------------------------------------------------------
even I know selectById is fast, but ....... I still want to ask you this question agian....
--------------------------------------------------------------------------------------
啊啊啊啊啊啊
刚写完这贴子忽然想起来您已经解决过这个问题了!!!!!-
--------------
这样就行了
--------------

var x=vtest.New();
            x.qx = "1";
            x.Save();

            var x3 = vtest.New();
            x3.qx = "2";
            x3.Save();

            var n1 = vtest.New();
            n1.Id = 1;

            var x1 = vtest2.New();
            x1.test = n1;
            x1.Save();

            n1.Id = 2;

            var x2 = vtest2.FindById(1);
            x2.test = n1;
            x2.Save();

-----
may be you should update the wiki...

Mar 17, 2009 at 3:20 AM

个人觉得,还有一种有趣的做法,如果两个对象是存在关系的,你可以在 vtest 里加入
[HasOne]
public abstract vtest2 test2 { get; set; }


[HasMany]
public abstract HasMany<vtest2> test2s { get; set; }

新建或者更更新对象时,可以从父级调用

            var x=vtest.New();
            x.qx = "1";

  var x1 = vtest2.New();
  var x2 = vtest2.FindById(1);

----------[HasOne]情况---

x. test2 =x1;

x.Save();

x1会自动保存。

----------[HasMany]情况---

x.test2s.Add(x1);

x.test2s.Add(x2);

x.Save();// 保存一次

x1和x2会自动保存。

------------------------------

    嘿嘿,不知道我说的对不对~~