用不同的属性替换EF实体

时间:2014-03-13 14:37:07

标签: c# asp.net-mvc entity-framework

我有一个存储在当前ID为1的VARBINARY中的excel文件。我需要用更新版本替换此文件,该文件必须保留在ID 1.我的解决方案下面确实替换了byte []内容,但更新了文件是不可读的。有任何想法吗? - ReadFileFully只从内存流中返回一个字节数组。

    public ActionResult ReplaceFORATVersion()
    {

        string file_name = "C:\\Calculation Engine FORAT.xls";
        FileStream fs = new FileStream(file_name, FileMode.Open, FileAccess.Read);

        var excelObj = db.FPTExcel.OfType<FPTFORATExcel>().Where(e => e.Id == 1).FirstOrDefault();

        excelObj.Content = ExcelManager.ReadFileFully(fs);

        db.SaveChanges();

        return RedirectToAction("Index", "AdminData");
    }

1 个答案:

答案 0 :(得分:0)

很可能与excelObj.Content = ExcelManager.ReadFileFully(fs);

行有关。

我会想象当你将文件转换为二进制数组时,缓冲区中有一些东西让它变得不快乐。

您可以将以上内容替换为:

public ActionResult ReplaceFORATVersion()
{

    string file_name = "C:\\Calculation Engine FORAT.xls";

    excelObj.Content = File.ReadAllBytes(fileName);

    db.SaveChanges();

    return RedirectToAction("Index", "AdminData");
}

请参阅此帖子以获得更好的主意:

Best way to read a large file into byte array in C#?