Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a wcf data service wired up to an EF4 entity model. As part of the saving of one of my entities, I want to run some custom logic.

To do this I've used a change interceptor which calls out to custom logic prior to the entity being persisted to the database. The problem however, is that some of my logic requires the entity id of the object being saved, which, if the operation is an insert, hasn't been generated yet.

So the question is, how do I run custom logic from my WCF data service, after the entity has been persisted to the database?

Thanks in advance for any replies

Ryan
Posted

The OP was from last year but for whom it may concern in the future I would suggest using transactions. So, wrap the insert in a transaction, you'll be able to get your id by an @@identity being returned from say a stored procedure, do whatever with the id and if things are good with it then commit the transaction, else don't and the record won't be inserted.
 
Share this answer
 
Comments
Ryan Gamal 18-Jul-11 11:51am    
Unfortunately WCf Data services don't work like that, you pass your data to a url, and the service (with the help of an EF entity model) saves it away itself. There are no stored procedures and no code to perform the save. So thus, where would you put a transaction?
Kent K 18-Jul-11 14:17pm    
Granted, my experience with WCF and transactions doesn't involve an EF model, but I wanted to provide notice on how your problem could be solved (again, I probably should have stated that it is a solution that isn't using EF model but for people finding your post for/by other means. .).
So, I have a system where I've built WCF services that run in IIS. I can pass custom objects of my choosing to them or retrieve objects from them like you would expect. My sytem is 3-tier. A desktop client is, well, the client. The WCF service (middle) uses SQL server for the backend. In the client I can wrap 7-8 different WCF service calls, some of which require an id from a foreign key field to be able to do an insert in a related table, in a transaction. With MSDTC configured in IIS and SQL server, a transaction will flow from client to service to db and all the way back to the client such that if there is a problem anywhere it simply rolls back and I don't have to do any messy "unwinding" of partial record inserts and what have you, myself.
That's the scenario I'm familiar with with behavior you questioned about. I'm not sure what level of control you have with EF models and their generated services, for transactions. Perhaps you'll have to make a service manually to do the task you are needing to be done instead of relying on the EF model to do it? Or hopefully there is a way to tell the EF model to make the service and a particular service method "transactional" in nature. The service and service methods do need to be marked with attributes to get it all to work.
As far as I can tell, this doesn't look possible. The best workaround is to use a standard WCF service for situations where you need to call out to business logic, or have more control over the save process. Data services are best served in scenarios where you do not need to run custom logic as part of the saving process.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900