Entity Framework 6 DBContext disable delete option

2019-07-31 03:54发布

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.

2条回答
我命由我不由天
2楼-- · 2019-07-31 04:27

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)

查看更多
Rolldiameter
3楼-- · 2019-07-31 04:42

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.

查看更多
登录 后发表回答