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

How do I return an object with a multiple field primary key?

5 Answers 72 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.
richardFlow
Top achievements
Rank 1
richardFlow asked on 09 Mar 2010, 10:24 AM
Hi,

I want to write a simple method for returning an object that is specified using multiple fields.

the code i use to return an object with a single field primary key is as follows:

  return (Product)scope.GetObjectById(Database.OID.ParseObjectId(typeof(Product), ProductKey));

However, with what do I replace the string ProductKey when the object has several fields, say KeyA, KeyB and KeyC required to select it?

Thanks!
          

5 Answers, 1 is accepted

Sort by
0
Matt.F
Top achievements
Rank 1
answered on 10 Mar 2010, 11:00 AM
Hi there,

Could you not use LINQ and do something along the lines of:

 return (from prod in Scope.Extent<Product>() where prod.Key1 ==  Value1 && prod.Key2 == Value2 && prod.Key3 == Value3  select prod ).First();

I'm kinda new to this whole ORM business so if I'm suggesting something which is crazy wrong please correct me so that I can make sure to correct my code.

Matt
0
richardFlow
Top achievements
Rank 1
answered on 12 Mar 2010, 08:43 AM
Hi,

Yes, that is currently how we implement the functionality, but from reading the guide i'm of the impression i lose out on all the benefits of the caching in memory of the objects etc...  The GetBojectById only goes to the database if it needs to.

Richard.
0
Zoran
Telerik team
answered on 15 Mar 2010, 09:37 AM
Hi Web Belief Ltd,

There are classes that implement the IObjectId interface, generated for each of the persistent classes with multiple identity. You could use those types of objects to get persistent objects by their identity. Here is an example with an OrderDetail persistent object which has composite key from the orderID and productID fields.

scope.GetObjectById<OrderDetail>(new OrderDetail.ID() { orderID = 1, productID = 3 });


Regards,
Zoran
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
richardFlow
Top achievements
Rank 1
answered on 18 Mar 2010, 10:52 AM
Thanks Zoran,

I tried that code but got this error: 
The non-generic method 'Telerik.OpenAccess.IObjectContext.GetObjectById(Telerik.OpenAccess.IObjectId)' cannot be used with type argument

I changed it to this and it worked:
(OrderDetail)scope.GetObjectById(new OrderDetail.ID() { orderID = 1, productID = 3 });

Why did your code not work? Is the way I've done it as efficient? Does this method avail of the caching?

Also, is the method you suggested better than the code I use to return an object with a single field primary key (which I detailed in the 1st post)?

Thanks!
0
Zoran
Telerik team
answered on 22 Mar 2010, 01:45 PM
Hi Web Belief Ltd,

I guess that you are using an older version of OpenAccess, that is why the generic variant of the GetObjectById method is not visible on your side. Nevertheless the performance is not really effected in both cases - no matter if you use the generic or non-generic approach.

Regarding your later question - yes this method does consider the 1st and 2nd level cache and if you object is already cached - OpenAccess will return it from there instead of visiting the database.

Greetings,
Zoran
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
General Discussions
Asked by
richardFlow
Top achievements
Rank 1
Answers by
Matt.F
Top achievements
Rank 1
richardFlow
Top achievements
Rank 1
Zoran
Telerik team
Share this question
or