Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue when trying to read multiplte entity resultsets from a stored procedure

We using EF5.0 and code-first approach with MS SQL Server I have read an article http://msdn.microsoft.com/en-us/data/jj691402.aspx

and decided to try the same approach over our database

however, suppose that my stores procedure contains a query like this

SELECT * from [dbo].[MyEntities] as MyEntity
    where ID = @ID

and code in C# is

var entities = Context.ObjectContext.Translate<MyEntity>(reader, "MyEntity", MergeOption.AppendOnly);

I am getting at this point

An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: The EntitySet name 'MyDbContext.MyEntity' could not be found.

So, obviously it adds some context name as a prefix to the EntitySet name and instead of MyEntity is looking for MyDbContext.MyEntity in the result set.

What causes this behaviour and if there is any workaroud (because in example I referenced above it looks quite streightforward and simple and no specific manipulations is needed, except call db.Database.Initialize(force: false); (which I do in my code as well)

like image 913
Bogdan_Ch Avatar asked Jan 10 '23 06:01

Bogdan_Ch


2 Answers

I have the same exception, even I don't use aliases in my select functions. I ended up by using the overloaded method of Translate by passing only the reader. I already know the order of the select functions so I do not need to pass the EntitySet name.

var entities_1 = Context.ObjectContext.Translate<MyEntity_1>(reader);
reader.NextResult();
var entities_2 = Context.ObjectContext.Translate<MyEntity_2>(reader);
like image 119
Bassem Akl Avatar answered Jan 29 '23 16:01

Bassem Akl


Well I find an answer myself If seems that table aliases not working and code-first mapping configurations between the entity classes and table name also not taken into account when you call Translate

So I need to specify original table name (MyEntities) instead of MyEntity in this line of code

var entities = Context.ObjectContext.Translate<MyEntity>(reader, "MyEntities", MergeOption.AppendOnly);
like image 36
Bogdan_Ch Avatar answered Jan 29 '23 14:01

Bogdan_Ch