Read options only (no change tracking, SaveChanges should throw away) EFv4 model?

NOTE: VS2010 / .NET 4, but without a beta version of SP1 at the moment, but if something in SP1 addresses this, it would still be great to know. :)

I have a specific database from which I want to read, but never write. I do not see anything in the properties of the model (looking in the EF designer) to mark it as read-only.

Of course, it seems that the easiest way is to add an incomplete class for the generated ObjectContext subclass (FooModel: ObjectContext class) to override SaveChanges (SaveOptions) and make it quit (maybe connecting to SavingChanges and throwing will work too, I haven't even tried). This will not disable change tracking, and AFAICT I will have to iterate over all entity sets when creating a context instance to mark them read-only (although this problem is not so critical, t persist).

Of course, there are other parameters, such as connecting the connection string as a user with read-only access, but I wonder what parameters are available from the point of view of the designer / model / EF instance for read-only object contexts?

+3
source share
1 answer

There are only a few ways to make your model read-only.

  • Object without primary key is read-only
  • An object mapped to a database view (without a primary key) is read-only. The same applies if you use DefiningQuerySSDL directly.
  • An object converted to QueryViewin CSDL is read-only. QueryViewonly works on other objects, so this general solution does not solve your problem.

Your proposal with user permissions to the database looks like the most secure way. If you want to prevent saving changes overriding SaveChangesand throwing something like NotSupportedExceptionalso looks like a good solution.

, , MergeOptions.NoTracking.

+3

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


All Articles