tags:

views:

37

answers:

2

Its a simple question, but I'm not aware of the answer and I couldn't get it to work.

Can I update only one entity on the entire DataContext? Or should I follow plain ADO.NET for this operation only?

Edit:

public MyObject GetMyObjectById(int selectedId)
    {
        DataContext db = _dbManager.GetContext();

        return db.MyObject.SingleOrDefault(p => p.Id == selectedId);
    }

I am getting an object with the above query... I am querying then for an integer...on another table/object

 public int GetMyInteger()
    {
        DataContext db = _dbManager.GetContext();

        return db.MyAnotherObject.FirstOrDefault().MyInteger;
    }

Everything is fine for all my operations...but now i just want to update only the integer i got from the database...

 public void SetMyInteger(int updInteger)
    {
        DataContext db = new DataContext(ConnectionString);

        MyAnotherObject theEntity = db.MyAnotherObject.FirstOrDefault();
        atheEntity.MyInteger = updInteger;
        db.SubmitChanges(ConflictMode.ContinueOnConflict);
    }

The above method deleted MyObject i got from the first query!!! Of course if i use the static context DataContext tries to update MyObject and MyAnotherObject which seems the correct behaviour.

Edit:

I have changed the method getting the integer with a new datacontext as well and seems to working correctly, i have a strange thought on why called the delete method, because it was the method that was called, but again .. is working now...

Thank you all for your time.

+3  A: 

Yes it's possible. What have you tried? It should be as simple as this:

using (var dc = new YourDataContext())
{
    Person p = dc.Persons.Take(1).Single();
    p.FirstName = "Ahmad";
    dc.SubmitChanges();
}
Ahmad Mageed
Yes tried but in one way where i use same datacontext for all my operations and while i have loaded some other objects, i get error from the other objects in the context, then tried doing this with a new DataContext and it deleted me the other object from the DataBase!!! you realize the bad of this :) but updated what i wanted too...so i think i need the way of updating only the entity i need ....somehow
@gtas: This answer demonstrates quite clearly the solution: you need to create a new datacontext when you start a new operation / transaction.
Mark Byers
@gtas if you're referencing multiple objects from the same DataContext and make changes to them, then all changes will be sent once you use `SubmitChanges()`. The simplest option is to use a single DataContext for the update. Or you can try performing the update you need, call `SubmitChanges()`, then continue with your work on the other items.
Ahmad Mageed
Strange is i tried with a new datacontext and Deleted me the other object i had loaded with another datacontext...xmmmmm
@gtas if these suggestions don't work then please update your question with an example of your code so we can have a better idea of the problem.
Ahmad Mageed
@gtas - are you attempting this in a web based scenario? It kind of sounds like multiple objects are being inserted or updated via the same data context. If you are running in to problems in a web scenario, you may want to try and create just one data context per request.
Eric
i am choosing this as a correct answer because here is the conversation Mark was right as well and helped of course as everyone thank you..
+1  A: 

Yes, you can:

Foo foo = dc.Foos.Where(foo => foo.Id == 345).Single();
foo.Name = "foo";
dc.SubmitChanges();
Mark Byers