我正在使用代码优先方法在 MVC 4 中插入数据,但是,我需要上传一个文件并使用存储过程保存它的名称,问题是,数据已成功保存在数据库中,但每次,屏幕都会显示此错误
控制器方法代码为:
[HttpPost]
public ActionResult myFile(HttpPostedFileBase file, String fileName )
{
if (file.ContentLength > 0)
{
fileName = Path.GetFileName(file.FileName);
string ext = Path.GetExtension(fileName).ToLower();
if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif")
{
if (file.ContentLength > 0.2 * 1024 * 1024)
{
ViewBag.uploadFailMessage = "Invalid File Size, It must be less than 1 MB";
}
else
{
var path = Path.Combine(Server.MapPath("~/uploads"),fileName);
file.SaveAs(path);
// Saving in DB
DBClass db = new DBClass();
//var postfrm = db.myFile.Add(upload);
//var postForm = db.myFile.Add(filee);
var postForm = (myFile)db.Database.SqlQuery<myFile>("uploadSP @fileName",
new SqlParameter("fileName",fileName)
).FirstOrDefault();
// /Saving in DB
ViewBag.uploadSuccessMessage = "File uploaded Succesfully";
}
}
else { ViewBag.uploadFailMessage = "Invalid File"; }
}
return View();
}
型号代码:
namespace MyBook.Models {
public class myFile
{
[Key]
public Int64 fileId { get; set; }
public String fileName { get; set; }
}}