2

Possible Duplicate:
How to make Entity Framework Data Context Readonly

Is there a configuration setting or simple way to make Entities "read-only" I have a data access layer that goes against a db that is the back end to a CRM solution. We don't want to write (accidentaly or purposely) to the db outside of the CRM UI but we do want to read data via several custom apps.

Community
  • 1
  • 1
John S
  • 7,909
  • 21
  • 77
  • 145
  • 1
    Have a look at a similar discussion here http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/5fb8d970-1131-4de7-a7fa-6cd1d7839e84/ – Maxim Zabolotskikh Nov 09 '12 at 15:21

2 Answers2

5

For your custom apps have the connection string point to a database user that only has read-only access. You will get errors if they then try to write to the database.

http://www.joellipman.com/articles/microsoft/sql-server/454-create-read-only-database-user-in-sql-server.html

1

I elected to override SaveChanges and throw an exception for the time being.

On second thought and after some good feedback I am not going to do this.

John S
  • 7,909
  • 21
  • 77
  • 145
  • That's just a nasty hack. And what I mean by that is that it will probably come back and bite you in the ass sooner or later (unless it's just you and a few other guys who are all well aware of this hack). – Anders Arpi Nov 09 '12 at 15:26
  • 1
    I disagree with the "nasty hack" assessment. This is why virtual methods exist, so they can be overriden if the behavior does not fit your scenario. Anders' solution is to simply trust your developers not to do something. The override is to trust *and verify*. Which is safer? Trust? Or verification? That said, if you can, go with the user permissions based approach at the database itself. – Anthony Pegram Nov 09 '12 at 15:34
  • The way our db is setup I think that I may go the route of wrapping the tables I need in Views and then making my Entities from those along with using a user that has read-only access to the DB. Thoughts? – John S Nov 09 '12 at 15:37