Update changes from class

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
JohnDoeP
Posts: 2
Joined: Tue 12 Jun 2012 14:33

Update changes from class

Post by JohnDoeP » Tue 12 Jun 2012 14:41

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

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Update changes from class

Post by MariiaI » Thu 14 Jun 2012 13:01

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

JohnDoeP
Posts: 2
Joined: Tue 12 Jun 2012 14:33

Re: Update changes from class

Post by JohnDoeP » Wed 18 Jul 2012 13:48

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

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Update changes from class

Post by MariiaI » Thu 19 Jul 2012 13:40

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.

Post Reply