Entity Framework function import: input parameters mapping
Entity Framework allows to map the result of a stored procedure to an Entity easily. What I need is to map an Entity to input parameters, so that instead of
context.SaveUser( user.FirstName, user.LastName, ... );
I can simply call it like this:
context.SaveUser( user );
What I really want is to isolate possible schema changes as much as I can. I'm only using EF to generate entities and function imports; the entire i开发者_运维百科nteraction with DB is performed through function calls. So whenever User table changes, I want to regenerate User entity in visual designer and change business logic code as appropriate; I do NOT want to change the data access layer. Currently, I'm not seeing any way around those property set dependent calls from data access layer to EF (like the one I posted above), which is a shame, since those could easily be regenerated along with entity classes.
Is there any other strategy which would allow me to achieve the same? The reason I'm using those stored procedures is actually because I want to have full control over SQL (maybe I'm just being paranoid, but it's kind of scary to end up with piles of LINQ code with little or no way to control actual SQL).
Is such thing possible?
Thank you
I don't think it is possible without some additional boilerplate code.
Maybe you should re-think your current access strategy and not use Stored Procedures. Then you will be able to do it like want.
This is possible but in different way than you are trying to achieve it. Each entity allows mapping of Insert, Update and Delete actions to stored procedures. So in your case, I assume you need to create mapping for Insert and Update action to SaveUser stored procedure. When mapping is done stored procedure is called internally each time the entity is added or updated in entity set and SaveChanges is requested. So you will work with entity set in the same way as without stored procedures. Check these articles on MSDN: How to and Walkthrough.
Edit:
Try to use mentioned approach and save changes this way:
var user = new User();
FillUser(user); // fill modified data to user entity
using (var context = new MyContext())
{
context.Users.Attach(user);
context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
context.SaveChanges(); // This should call your Update stored procedure
}
精彩评论