4

Please forgive me if this question has been asked and answered already. If so, kindly point me to it. I'm using VS2010 over .Net 3.5 (platform update not in my control) with up-to-date Service Packs.

I have a stored proc that will return rows like the following. There is no other column that could function as a Key for this entity; the results look like this :

COUNTRYCODE     QUANTITY
USA             100
UK              250
USA             300
USA             190

and so on.

I followed the steps from http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/13/mapping-stored-procedure-results-to-a-custom-entity-in-entity-framework.aspx

as well as http://msdn.microsoft.com/en-us/library/cc982038(v=VS.90).aspx

This keeps resulting in the error "Entity Type soAndso has no key defined. Define the Key for this Entity Type"

How do we mitigate this error? I just don't have a need for a primary key here and can't modify the stored procedure. I'm asking SOF as a last resort. Please help. Thank you very much.

FMFF
  • 1,652
  • 4
  • 32
  • 62

1 Answers1

5

If you don't have need for a primary key, then I'm assuming you don't have a requirement to be able to modify this data and submit it back to the server. In this case, you should use a Complex Type instead of an Entity type. Complex types are classes and can serve as DTO's just as well as entities can, but you can't use complex types as basic Insert/Update/Delete objects.

EDIT: It appears that complex types may not be usable in EFv1 (.NET 3.5) except as a means of grouping scalar properties on entity types. Unfortunately EFv1 is vastly inferior to EFv4 and there simply may not be a solution to your problem that involves using the Entity Framework.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 1
    Yeah, this should work.. Here is more info: http://msdn.microsoft.com/en-us/library/bb738472.aspx http://msdn.microsoft.com/en-us/library/cc716733.aspx – pirho Jul 13 '11 at 22:18
  • Thank you Adam. You are correct in your guess that, the SP performs only a SELECT. No UPDATE/INSERT/DELETE involved. That said, complex types aren't available from VS2010 if I choose .Net 3.5 as my target platform. And the machine where this code will be deployed doesn't have 4.0 installed. :-( What do I do? – FMFF Jul 13 '11 at 22:19
  • 1
    How about: http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/13/mapping-stored-procedure-results-to-a-custom-entity-in-entity-framework.aspx – pirho Jul 13 '11 at 22:21
  • 1
    @FMFF: After looking at @pirho's link, it sounds like complex types may have been much more limited in EFv1. I can't speak from experience since I stayed away from that version after all of the talk online, but if that's the case then there may not be a solution to your problem, unfortunately. Does the EF designer only allow you to map to entity types? Posting a screenshot might be helpful. – Adam Robinson Jul 13 '11 at 22:23
  • @pirho: That's mapping to an entity, not a complex type (or anything other than an entity). – Adam Robinson Jul 13 '11 at 22:24
  • @pirho, yeah, that article was one of my references for this dev effort. In that article he uses "CourseID" as a key to that entity. I do not know how I can do something similar for the rows that look like the above. – FMFF Jul 13 '11 at 22:24
  • Yeah, true... Can you introduce your own SP which encapsulates existing one and return result which has "ID"-field which is from OVER-function as in Behnam's answer. It's hax, but works :/ – pirho Jul 13 '11 at 22:31
  • I think the powers that be will be more open to updating the SP than updating to .Net 4.0. I will try @Behnam's suggestion and will post an update here. – FMFF Jul 13 '11 at 22:34