This is a migrated thread and some comments may be shown as answers.

selective commit.

10 Answers 100 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andrey
Top achievements
Rank 1
Andrey asked on 16 Jun 2011, 08:23 AM
Hello Telerik team.
I have a list of objects from DB: 
IList<Entity> myList = from p in scope.Extent<Entity>() select p;
Initially this list - is a list of some Standard objects.
During work user can change all of them. BUT if he want to save his changes - he can save only "Save as..."
So, he can't reWrite Standard objects. Only create new (his own object) and then Save (Commit).
So, before Commit() I create a copy of Standard object
var userEntity = new Entity();
StandardEntity.CopyTo(userEntity);
and now I should Add userEntity to a scope and Commit();
but How I can NOT COMMIT Standard object???
Because now it is Changed too. Because user changed it earlier. Before Click <Save as...>
I know about 
transaction.Rollback();
But I need Use Changed Standard object during this session. But without save to DB.
And when user load all Standard objects from DB next time - he must look Standard objects - not changed.

Is it possible to realize?
Thanks!

10 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 16 Jun 2011, 09:34 AM
Hi Andrey,

You could try using the Refresh method on the Standard object before commit. It will "clean" the instance with the information from the database.

Regards

Henrik
0
PetarP
Telerik team
answered on 21 Jun 2011, 02:32 PM
Hi Andrey,

 What Henrik suggested should do the trick for you. Basically when you commit our object tracking fires in and generates sql queries for all the objects that are market as "dirty" (modified in the given transaction). What the Refresh method does is to reset the original values of the object and remove the "dirty" status from it.

All the best,
Petar
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
IT-Als
Top achievements
Rank 1
answered on 24 Jun 2011, 09:13 AM
Hi Andrey,

If the answer above solved your issues, please mark it as answered so that other forum visitors will be able to find an answer to their question quickly.

Best regards

Henrik
0
LordRevan007
Top achievements
Rank 1
answered on 16 Aug 2011, 09:47 AM
I tried the suggested solution but with no effect. I use a postgresql database.
Any hints on how to do this?

Thanks in advance
0
IT-Als
Top achievements
Rank 1
answered on 16 Aug 2011, 09:51 AM
Hi,

What doesn't work?

Refresh should clear the dirty flags and thus omit the objects from being committed. Show us some code, please..

/Henrik
0
LordRevan007
Top achievements
Rank 1
answered on 16 Aug 2011, 10:27 AM
It doesn't clear the dirty flag. I have two tables with an 1 to many relationship in them.
For example:
Product(idProduct, name, idProductType)
ProductType(idProductType,name).
If I want to add a product with a given product type from the db it will add a new product type also.
I tried with ctx.refresh in all sort of combinations but still with no result.

My code looks like the following snippet.

var t=new Product();
t.name=
"test";
var tp=managerProductType.GetProductTypeById(1);
t.ProductType=tp;
ctx.Add(t);
ctx.Refresh(RefreshMode.OverwriteChangesFromStore,t.ProductType)
ctx.SaveChanges();


Thanks in advance
0
IT-Als
Top achievements
Rank 1
answered on 16 Aug 2011, 10:37 AM
Hi,

How is the 1:m relationship mapped... with an inverse field (foreign key column in the table representing the ProductType entity)?   Or a join table (like a product_producttype table)?

When you go and get the ProductType entity from the database and NOT add it to the context with the Add method it should work.
However, if your 1:m is mapped with an inverse field only 1 product can reference the same product type...  I guess what you want to achieve is the product to have exactly 1 (of some predefined( already existing in database) product type, right?

if so this is a either a 1:m with a join table or a m:n....   Eventually see the knowledge base article on mapping 1:1 relationsships.

Regards

Henrik
0
LordRevan007
Top achievements
Rank 1
answered on 16 Aug 2011, 10:57 AM
The relationship is mapped with an inverse field. In my case the Fk key from Products idProductType references to idProductType from my ProductType table. If I send only the idProductType works, but if I send the object ProductType that is associated with Product it add that ProductType also.  What I want is not to add that ProductType.

0
IT-Als
Top achievements
Rank 1
answered on 16 Aug 2011, 11:06 AM
Hi,

Try to map it as a 1:m with a join table instead.

The "problem" with the inverse field is that OA thinks that the "owner" of the list (the 1 side) must be saved, too, if you change (add an item) the list (the many side).

Regards

Henrik
0
LordRevan007
Top achievements
Rank 1
answered on 16 Aug 2011, 11:07 AM
Thanks I'll give it a shot.

LordRevan007
Tags
General Discussions
Asked by
Andrey
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
PetarP
Telerik team
LordRevan007
Top achievements
Rank 1
Share this question
or