ADO.net, Check if ObjectContext is writeable
I have an embedded database in an asp.net mvc project. If I 开发者_如何学Gotry 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?
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
精彩评论