开发者

Updating an entity with linq to sql, do I need to call attach first?

My code looks like:

Entity e = new Entity();

e.name = "...";
e.blah = 234;

MyDb.UpdateEntity(e);


public static void UpdateEntity(Entity e)
{
    using(MyDatacontext dc = new MyDataContext())
    {

          dc. ?????????
    }

}

So what do I do here to update the ent开发者_如何学City?

note: i just called it entity here, its something else in my project.


It really depends on your data context. Normally you will have an object for each table in your database. So for example if you have a database that has an orders table you will have an Orders object in your DataContext (created by dragging the table into your dbml file in the designer).

So for a new order you woud do the following:

using (var ctx = new MyDataContext()) {
    ctx.Orders.InsertOnSubmit(order);

    ctx.SubmitChanges();
}

And to save an order passed to your client and modified ther you do:

using (var ctx = new MyDataContext()) {
    ctx.Orders.Attach(order, true);

    ctx.SubmitChanges();
}


dc.GetTable<Entity>().InsertOnSubmit(e);
dc.SubmitChanges();

http://msdn.microsoft.com/en-us/library/bb763516.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜