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
Update changes from class
Re: Update changes from class
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:
The TableTest entity type is generated by the following table:
For more information about the Attach() method please refer to:
http://www.devart.com/linqconnect/docs/ ... 00%29.html
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();
Code: Select all
CREATE TABLE `Table_test`
(
id INTEGER NOT NULL,
prop INTEGER,
PRIMARY KEY(id)
)
http://www.devart.com/linqconnect/docs/ ... 00%29.html
Re: Update changes from class
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
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
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:
Please notify us about the results.
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();