Ayya 2K
Ayya 2K

Reputation: 13

Entity Framework 6 DBContext disable delete option

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.

Upvotes: 1

Views: 871

Answers (2)

Pawel
Pawel

Reputation: 31610

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)

Upvotes: 1

Saanch
Saanch

Reputation: 1844

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.

Upvotes: 0

Related Questions