ADO.net, Check if ObjectContext is writeable

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?

Answers


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


Need Your Help

Why does this code not work in IE & Safari?

javascript html browser

I've been tasked with writing some code, that places a small icon in the top right corner of an Ad regardless of size. Depending on the language of the user (in this case I'm using the browser lang...