使用文件流LINQ删除大文件

本文关键字:文件 删除 LINQ | 更新日期: 2023-09-27 18:12:17

使用Filestreams在LINQ中删除文件非常简单:

Table<File> f = dac.GetTable<File>();
var itemToDelete = f.Single(x => x.fileId == fileId);
f.DeleteOnSubmit(itemToDelete);
dataContext.SubmitChanges();

当文件变得相当大时,问题就出现了。

异常详细信息:System.ComponentModel。Win32Exception:等待操作超时

我目前的解决方案是一个变通:

dataContext.CommandTimeout = 1000;

但我真的不喜欢这个解决方案。我不需要获取内容,这就是为什么超时发生在第一时间。

这个问题有一个好的干净的解决方案吗?

使用文件流LINQ删除大文件

这个博客提供了一个很好的解决方案,甚至不使用SqlQuery

// Create an entity to represent the Entity you wish to delete 
// Notice you don't need to know all the properties, in this 
// case just the ID will do. 
Category stub = new Category { ID = 4 }; 
// Now attach the category stub object to the "Categories" set. 
// This puts the Entity into the context in the unchanged state, 
// This is same state it would have had if you made the query 
ctx.AttachTo("Categories", stub); 
// Do the delete the category 
ctx.DeleteObject(stub); 
// Apply the delete to the database 
ctx.SaveChanges();

我将直接针对上下文运行SQL,例如

dataContext.ExecuteStoreCommand('SQL TO DELETE BLOB')