tags:

views:

25

answers:

2

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 entity?

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

+1  A: 
dc.GetTable<Entity>().InsertOnSubmit(e);
dc.SubmitChanges();

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

Mark
`dc.InsertOnSubmit(e)` is not valid, as the `InsertOnSubmit` operation is a method of the Table class. I.e. you need to do `dc.Etities.InsertOnSubmit(e)`.
Obalix
@Obalix Yea..I saw that too. Woops. :)
Mark
@Obalix `dc.Entities` would be cleaner, but I like having a generic repository, so `GetTable<T>` works better for me.
Mark
+3  A: 

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();
}
Obalix
+1 for attaching. I always forget about that.
Mark