简体   繁体   中英

Entity Framework - Stored Procedure result as an entity without Primary Key

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM