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)
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With