Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq-to-SQL How to prevent the use of Delete methods?

By convention our DB only alows the use of stored procedures for INSERT, UPDATE and DELETE. For some tables / types there is no DELETE stored procedure, because it is not allowed to delete rows. (You can only update the status of such a type to "deleted"). e.g. a customer may be marked as deleted but is never really removed from the DB.

How do I prevent the use of Delete() for certain types in the Data Access Layer = in the DMBL?

The "Default Methods" for Insert and Update are mapped to the corresponding stored procedure. But for Delete it says "use runtime". I would like to set it to "not allowed".

Is there a way to achieve this on the DB model layer?

Many thanks

like image 241
PeterFromCologne Avatar asked Jan 25 '23 00:01

PeterFromCologne


1 Answers

Implement a partial class for each such entity and implement the OnValidate partial method. It takes a ChangeAction as a parameter. When the ChangeAction is ChangeAction.Delete, throw an exception that indicates that the operation is disallowed (IllegalOperationException, maybe).

like image 148
tvanfosson Avatar answered Jan 26 '23 14:01

tvanfosson