Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Entity Framework read-only

I'm currently creating an application that extracts data from lots of different data sources - most of these are development databases but some are live (I understand this is awful, but it's completely out of my control).

I'd like to protect myself from making modifications to these live databases, I'm therefore interested in making entities read-only.

My research tells me this isn't possible, but I'm anticipating a feature change or work-around may be available now. Has anyone achieved this?

like image 511
m.edmondson Avatar asked Feb 21 '23 05:02

m.edmondson


1 Answers

I think that it's better to put that limit in the database instead of in the application. Create a separate user in the live database, that is member of the data_reader role. That will give read access only to the database.

If you've tweaked the security settings and roles on the server you have to check more in detail, but as a default data_reader will do.

Edit

A simple way to do it would be to override the SaveChanges() method of the data context for those databases:

public override int SaveChanges()
{
    throw new AccessViolationException(
         "Don't mess with a live database during test");
}
like image 118
Anders Abel Avatar answered Feb 27 '23 08:02

Anders Abel