ADO.net, Check if ObjectContext is writeable
-
22-09-2019 - |
Question
I have an embedded database in an asp.net mvc project. If I try to write to the file, I sometimes get a write failed exception because the SQL Server can't write to the file. How can I check an ObjectContext, if its writeable, without actually writing something to the database?
Solution
You could execute something like this directly against the database to find out it if is read-only or not:
SELECT DATABASEPROPERTYEX('DatabaseName','Updateability')
To do that you would use:
- EF 4.0 =>
ObjectContext.ExecuteStoreCommand(..)
- EF 3.5 =>
(ObjectContext.Connection as EntityConnection).StoreConnection as SqlConnection
to get to the underlying database connection, and then create a SqlCommand.
Once you've figured this out, I'd probably turn this into an Extension method so you could do something like this:
if (ctx.ReadOnly()) ...
Hope this helps
Alex
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow