ADO.net,检查 ObjectContext 是否可写
我在 ASP.NET MVC 项目中有一个嵌入式数据库。如果我尝试写入该文件,有时会收到写入失败异常,因为 SQL Server 无法写入该文件。如何检查 ObjectContext(如果其可写)而不实际向数据库写入内容?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以直接对数据库执行类似的操作以查明它是否是只读的:
SELECT DATABASEPROPERTYEX('DatabaseName','Updateability')
为此,您可以使用:
ObjectContext.ExecuteStoreCommand(..)
(ObjectContext.Connection as EntityConnection).StoreConnection as SqlConnection
获取底层数据库连接,然后创建一个SqlCommand。一旦你弄清楚了这一点,我可能会把它变成一个扩展方法,这样你就可以做这样的事情:
希望这对
亚历克斯有帮助
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:
ObjectContext.ExecuteStoreCommand(..)
(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:
Hope this helps
Alex