Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping of stored procedures is all-or-nothing in Entity Framework? (FunctionMapping)

I got the error

Cannot find the InsertFunctionMapping for EntityType 'xxx' in the mapping file.

Which is fair enough because it is true. But that is because I am happy with EF doing inserts for me. I simply want to override the delete function.

I thought that was one of the improvements with EF4? Or is it just that it will build fine but still cry when you use the unmapped functions? Or is it possible but I am just missing something?

like image 772
BritishDeveloper Avatar asked Aug 16 '10 11:08

BritishDeveloper


2 Answers

AFAIK. It's all or nothing.

If you do not map all three of the insert, update, or delete operations of a entity type to stored procedures, the unmapped operations will fail if executed at runtime and an UpdateException is thrown.

MSDN

like image 155
Yury Tarabanko Avatar answered Sep 24 '22 04:09

Yury Tarabanko


I have the same or similar problem. I want to override only delete function, but it is not possible in EF4 without overriding also insert and update functions. In my case I need to do this action for delete:

update table set deleted = 1 where id = @id

I solved this by this way: Before calling the method context.SaveChanges(true) I run this code

foreach (ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
{
  entry.ChangeState(EntityState.Modified);
  entry.Entity.GetType().GetProperty("deleted").SetValue(entry.Entity, true, null);
}

I'm beginner in EF. Now I only looking for how to solve some problems with EF which I have to solve before I start to develop application based on EF. But now it looks like that this solution should work. I hope.

like image 22
petr3petr Avatar answered Sep 23 '22 04:09

petr3petr