Sql server 2008 r2 SQL文件流中断

Sql server 2008 r2 SQL文件流中断,sql-server-2008-r2,entity-framework-5,transactionscope,sqlfilestream,Sql Server 2008 R2,Entity Framework 5,Transactionscope,Sqlfilestream,场景:我最近在我的ASP.NET MVC应用程序中添加了一个组件,允许他们将文件上传到数据库中。因为这些文件平均超过2MB,所以我选择使用FILESTREAMs。我将HttpPostedFileBase保存到一个临时文件中,执行一些业务逻辑,然后上传该文件。上载后,用户将重定向到浏览器中内联的查看文件的页面。以下是相关的上传代码: var DocContext = new DocumentEntities(); var dbFile = new File { DocumentID = G

场景:我最近在我的ASP.NET MVC应用程序中添加了一个组件,允许他们将文件上传到数据库中。因为这些文件平均超过2MB,所以我选择使用FILESTREAMs。我将
HttpPostedFileBase
保存到一个临时文件中,执行一些业务逻辑,然后上传该文件。上载后,用户将重定向到浏览器中内联的查看文件的页面。以下是相关的上传代码:

var DocContext = new DocumentEntities();
var dbFile = new File
{
    DocumentID = Guid.NewGuid(),
    Name = fileName,
    Type = file.ContentType
};
DocContext.Document.Add(dbFile);
DocContext.SaveChanges();

using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
using (var sqlFS = dbFile.Open(DocContext, FileAccess.Write))
using (var tempFS = tempFile.OpenRead())
{
    tempFS.CopyTo(sqlFS);
    scope.Complete();
}
public ActionResult File(Guid? id = null)
{
    if (id == null)
        return RedirectToActionPermanent("Index");

    return File(DocContext.Document.Find(id.Value) as File);
}

private ActionResult File(File file)
{
    if (file == null)
        throw new HttpException(404, "Unknown document type");

    var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead });
    Disposing += d => { scope.Complete(); scope.Dispose(); };

    var fs = file.Open(DocContext, FileAccess.Read);
    Disposing += d => fs.Dispose();

    return new Misc.InlineFileStreamResult(fs, file.MimeType) { FileDownloadName = file.FileName, Inline = true };
}
以下是相关的查看/下载代码:

var DocContext = new DocumentEntities();
var dbFile = new File
{
    DocumentID = Guid.NewGuid(),
    Name = fileName,
    Type = file.ContentType
};
DocContext.Document.Add(dbFile);
DocContext.SaveChanges();

using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
using (var sqlFS = dbFile.Open(DocContext, FileAccess.Write))
using (var tempFS = tempFile.OpenRead())
{
    tempFS.CopyTo(sqlFS);
    scope.Complete();
}
public ActionResult File(Guid? id = null)
{
    if (id == null)
        return RedirectToActionPermanent("Index");

    return File(DocContext.Document.Find(id.Value) as File);
}

private ActionResult File(File file)
{
    if (file == null)
        throw new HttpException(404, "Unknown document type");

    var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead });
    Disposing += d => { scope.Complete(); scope.Dispose(); };

    var fs = file.Open(DocContext, FileAccess.Read);
    Disposing += d => fs.Dispose();

    return new Misc.InlineFileStreamResult(fs, file.MimeType) { FileDownloadName = file.FileName, Inline = true };
}
开放式方法:

public partial class File
{
    public SqlFileStream Open(DocumentEntities db, FileAccess access)
    {
        var path = db.Database.SqlQuery<string>(
            @"SELECT FileData.PathName() FROM [File] WHERE DocumentID = @docID",
            new SqlParameter("docID", DocumentID)).First();
        var context = db.Database.SqlQuery<byte[]>(
            @"SELECT Get_FILESTREAM_TRANSACTION_CONTEXT() FROM [File] WHERE DocumentID = @docID",
            new SqlParameter("docID", DocumentID)).First();

        return new SqlFileStream(path, context, access);
    }
}
公共部分类文件
{
公共SqlFileStream打开(DocumentEntities数据库,FileAccess)
{
var path=db.Database.SqlQuery(
@“从[File]中选择FileData.PathName(),其中DocumentID=@docID”,
新的SqlParameter(“docID”,DocumentID)).First();
var context=db.Database.SqlQuery(
@“从[File]中选择Get_FILESTREAM_TRANSACTION_CONTEXT(),其中DocumentID=@docID”,
新的SqlParameter(“docID”,DocumentID)).First();
返回新的SqlFileStream(路径、上下文、访问);
}
}
查看以前上传的文件效果很好。查看用户自己(最近?)上传的文件时,会出现以下异常:
无法执行事务操作,因为有挂起的请求正在处理此事务。

发生什么事了


更新:我假设因为我是SQL系统管理员,我可以上传文件并查看它们。

当数据库条目的类型列中有MIME类型而不是扩展名时,我使用IIS和web.config来推断文件的扩展名。但是普通用户没有读取web.config的权限。因此,当非服务器管理员用户试图查看以MIME类型而不是扩展名存储的文件时,我的例程将抛出权限异常,这将以某种方式触发事务操作异常。不知道怎么做