问题
Anyone please help me on how to disable DELETE option in EF6?
I mean from the application, now record should be deleted (even accidentally)
Thanks.
回答1:
Create a user/role in the database that does not have permissions to delete/modify records and use it in your application. EF itself is not meant to be a security tool and there are always options to perform a delete operation (e.g. a developer can send any arbitrary SQL query/command to the database bypassing all the 'security' measures implemented in the data access layer)
回答2:
When getting the entities call with AsNoTracking()
option.
eg :- Context.Users.AsNoTracking()
Edit after Stevens Comment
Its true that anyone can still go and change the entity state to Deleted manually. I would recommend, using Repository Pattern
for data access and can restrict delete operation. By hiding the DbContext outside of the assembly.
来源:https://stackoverflow.com/questions/20309150/entity-framework-6-dbcontext-disable-delete-option