Remove row from database and directory on disk in

2019-07-28 03:16发布

问题:

Possible Duplicate:
Deleting files and corresponding entries from database

I'm writing an ASP.NET application. I have a row in a database with physical path to a folder on disk. I want to delete the row and remove the directory in one transaction. I.e. make sure both are deleted or both are not.

I'm using Entity Framework to communicate with the database.

The folder to be removed contains images, which may be viewed by a web-site user. I'm afraid I could try to delete the folder at the moment an image is transferred to a user by IIS.

回答1:

For this you need to use Transaction Scope, if your server is Windows 2008 R2 or 2012 it will work because the Transaction will be over SQL and NTFS. Make sure the Distributed Transaction Coordinator windows service is in running state.