Page 1 of 1

Update changes from class

Posted: Tue 12 Jun 2012 14:41
by JohnDoeP
Hello @all!

I have question according to the dataContext. Is it possible to get one database object, modify them and insert the object without the initial dataContext? Can i attach this database object to another dataContext?

Best regards
John

Re: Update changes from class

Posted: Thu 14 Jun 2012 13:01
by MariiaI
LinqConnect allows attaching updated entities to a new DataContext object via the Attach(TEntity, TEntity) method. Keep in mind, that it is necessary to pass the 'old' data of the row being updated to the context to create the proper update statement.
For example:

Code: Select all

 MyDataContext InitialDt = new MyDataContext();
 TableTest entNew = (from t in InitialDt.TableTests select t).First();
 //  do some changes with 'entNew'
 entNew.Prop += 1;            
 // create new context  
 MyDataContext newDt = new MyDataContext() { Log = Console.Out };
  // get 'old' entity
 Context.TableTest entOld = (
      from t in newDt.TableTests
      where t.Id == entNew.Id
      select t)
      .First();
  // attach
  newDt.TableTests.Attach(entNew, entOld);
  newDt.SubmitChanges();
The TableTest entity type is generated by the following table:

Code: Select all

CREATE TABLE `Table_test` 
(
  id INTEGER NOT NULL,
  prop INTEGER,
  PRIMARY KEY(id)
)
For more information about the Attach() method please refer to:
http://www.devart.com/linqconnect/docs/ ... 00%29.html

Re: Update changes from class

Posted: Wed 18 Jul 2012 13:48
by JohnDoeP
Thank you for your answer. It is working great.

I have a further question regarding the attach function. Is it possible to “attach permanently” the items of a class to another class, which have an OneToOne association?

If I get an object of the class A. Change some properties of this object and also make changes to the “referential constraint property” (from class B), then save it like your sample above. After saving I see that the “referential constraint property” (from class B) is “DUPLICATED”. How can I avoid this duplicate object?

Best regards
John

Re: Update changes from class

Posted: Thu 19 Jul 2012 13:40
by MariiaI
When attaching a 'main' entity, the 'dependent' entity is implicitly attached as well. However, since the context has no information about the origin of the new 'dependent' entity, it assumes that the entity is a newly added object, which hence should be inserted into the database. To resolve the issue, you can attach both 'main' and 'dependent' entities as modified.

Try rewriting your code as follows:

Code: Select all

MainDataContext InitialDt = new MainDataContext();
MainEntity entNew = (from t in InitialDt.MainEntities select t).First();
//  Do some changes with main entity 'entNew'.
entNew.Property = newValue;

//  Get 'dependent' entity and make changes.  
DependentEnt depNew = entNew.DependentEnts;
depNew.Property = "newValue";

// Create new context.
MainDataContext newDt = new MainDataContext() { Log = Console.Out };

// Get 'old' main entity.
MainEntity entOld = (
    from t in newDt.MainEntities
    where t.ID == entNew.ID
    select t).First();        
// Get 'old' 'dependent' entity.
DependentEnt depOld = entOld.DependentEnts;

// Attach modified entities.
newDt.DependentEnts.Attach(depNew, depOld);
newDt.MainEntities.Attach(entNew, entOld);
newDt.SubmitChanges();
Please notify us about the results.