Make Entity Framework Read Only

I am currently creating an application that extracts data from a large number of different data sources - most of them are development databases, but some of them are live (I understand that this is terrible, but it is completely out of control).

I would like to protect myself from making changes to these active databases, so I'm interested in making read-only objects.

My research tells me this is not possible, but I expect that a function change or job may be available now. Has anyone achieved this?

+4
source share
1 answer

I think it is better to place this limit in the database rather than in the application. Create an individual user in a live database that is a member of the data_reader role. This will give read access only to the database.

If you changed the security settings and roles on the server, you will need to check in more detail, but by default there will be data_reader .

Edit

An easy way to do this is to override the SaveChanges() method of the data context for these databases:

 public override int SaveChanges() { throw new AccessViolationException( "Don't mess with a live database during test"); } 
+8
source

Source: https://habr.com/ru/post/1400553/


All Articles