views:

43

answers:

3

I want to update the only field of entity when I know entity Id.

Is it possible in LINQ to SQL without retrieving full entity (with all fields from DataContext that is overhead) ? Is it possible to create and attach entity to DataContext and mark the exact field(s) to synchronize on DataContext.SubmitChanges (or something like that)?

Thank you in advance!

+1  A: 

You can always create a standard T-SQL statement and execute that against your data store:

YourDataContext
  .ExecuteCommand("UPDATE dbo.YourTable SET ThatField = newValue WHERE ID = 777", null);

With Linq-to-SQL itself, you cannot do this - it's basic assumption is that it always operates on the object, the whole object, and nothing but the object.

If you need to do this on a regular basis, one way would be to wrap it into a stored proc and add that stored proc to your data context as a method you can call on the data context.

marc_s
OMG, it's a pity. Has situation changed in EF ? Would you personally rely on embedded T-SQL operators or Stored Procedures embedded in Model for such cases?
Andrew Florko
No - EF has the same approach - but so do NHibernate or any of the other OR mappers. They're designed to load, manipulate and save **objects** as a whole. I am not aware of any ORM that would allow you to retrieve just one or two fields, change and save those....
marc_s
in EF we can create in-memory entity, initiate EntityKey property + primary key property, then attach, then change properties we want to update. SaveChanges works for updates.
Andrew Florko
+1  A: 

You can refresh the object. This example will change the person's first name:

Person person = _entities.Persons.FirstOrDefault(p => p.Id == id);
person.FirstName = "Bill";
_entities.Refresh(System.Data.Objects.RefreshMode.ClientWins, person);
_entities.SaveChanges();
gnome
Thank you, @Gnome. But is it possible to update field without entity retrieving?
Andrew Florko
;-) I was just re-reading your post. As far as I know you will need the entity. But this solution avoids that old-school-sql.
gnome
@gnome: yes, it avoids the "old-school" SQL - but it will need to retrieve the full entity first, and will save back the full entity again. The "old school" SQL can get around that very easily.....
marc_s
+1  A: 

Yes you can:

Foo foo=new Foo { FooId=fooId }; // create obj and set keys
context.Foos.Attach(foo);
foo.Name="test";
context.SubmitChanges();

In your Dbml set UpdateCheck="Never" for all properties.

This will generate a single update statement without a select.

One caveat: if you want to be able to set Name to null you would have to initialize your foo object to a different value so Linq can detect the change:

Foo foo=new Foo { FooId=fooId, Name="###" };
...
foo.Name=null;

If you want to check for a timestamp while updating you can do this as well:

Foo foo=new Foo { FooId=fooId, Modified=... }; 
// Modified needs to be set to UpdateCheck="Always" in the dbml
chris
Sounds interesting. I'll give it a try in a few days!
Andrew Florko
Ah yes, similar approach works in EF. Though I have to generate EntityKey property as well as value for primary key property.
Andrew Florko