Entity Framework 6 DBContext disable delete option

一个人想着一个人 提交于 2019-12-24 14:26:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!