0

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?

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
user287107
  • 9,286
  • 1
  • 31
  • 47

1 Answers1

0

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

Alex James
  • 20,874
  • 3
  • 50
  • 49