Hirasawa Yui
Hirasawa Yui

Reputation: 1296

Apply changes to a specific table only

There is a code that uses a single linq context objects to work with multiple tables. After changing some values, I need to update the them in DB. Is it achieved with a SubmitChanges function, that applies all the changes that made. The code looks kind of like this:

MyDBDataContext ctx = new MyDBDataContext(connectionString);

first_table_object first = (from f in ctx.first_table_objects select f).FirstOrDefault();
first.property = 1;

second_table_object second = (from s in ctx.second_table_objects select s).FirstOrDefault();
second.property = 2;

//ctx.SubmitChanges();
ctx.SubmitChangesOnlyToTheSecondTable();

Is it possible to update only one table instead of submitting all the changes?

Upvotes: 1

Views: 104

Answers (1)

Perfect28
Perfect28

Reputation: 11317

For this purpose you can detach the object retrieved from the first table. You can achieve it by using AsNoTracking(). This is will instruct EF to no take care of changes made on entities taken from first_table_objects

 first_table_object first = (ctx.first_table_objects.AsNoTracking().Select(s => s)).FirstOrDefault()

Upvotes: 1

Related Questions