'HttpPostedFileBase'没有定义键

时间:2015-09-22 11:50:43

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

我有一个Web应用程序可以将图像上传到数据库并进行检索。

public class ImageGallery
{
    [Key]
    public int ImageID { get; set; }
    public int ImageSize { get; set; }
    public string FileName { get; set; }
    public byte[] ImageData { get; set; }
    [Required(ErrorMessage="Please select Image File")]
    public HttpPostedFileBase file { get; set; }
}

我的数据库上下文类是这样的

public class MyDatabaseEntities : DbContext
{
    public DbSet<ImageGallery> ImageGalleries { get; set; }
}

这是我的控制器

public ActionResult Upload()
{
    return View();
}
[HttpPost]
public ActionResult Upload(ImageGallery IG)
{
    IG.FileName = IG.File.FileName;
    IG.ImageSize = IG.File.ContentLength;
    byte[] data = new byte[IG.File.ContentLength];
    IG.File.InputStream.Read(data, 0, IG.File.ContentLength);
    IG.ImageData = data;
    using(MyDatabaseEntities dc = new MyDatabaseEntities())
    {
        dc.ImageGalleries.Add(IG);
        dc.SaveChanges();
    }
    return RedirectToAction("Gallery");
}

现在,当我尝试上传图片时,它会给我以下错误

  

EntityType'HttpPostedFileBase'没有定义键。定义此EntityType的键。 HttpPostedFileBases:EntityType:EntitySet'HttpPostedFileBases'基于类型'HttpPostedFileBase',没有定义键。

我已经看到了关于堆栈溢出的问题之一----- 'HttpPostedFileBase' has no key defined. Define the key for this EntityType。我尝试了解决方案,但没有取得任何成功。

为此目的,我正在关注此博客------ http://dotnetawesome.com/mvc/how-to-upload-image-to-database-and-show-in-view-without-image-handler

3 个答案:

答案 0 :(得分:2)

试试这个

[NotMapped]
public HttpPostedFileBase File { get; set; }

这不会映射到数据库。

发生错误是因为表中没有数据类型HttpPostedFileBase

答案 1 :(得分:1)

您不能将HttpPostedFileBase存储在数据库字段中(它是包含多个属性的复杂对象)。您可以使用[NotMapped]属性排除此项,但您的模型和视图实际上没有任何关系(您不包括模型其他属性的任何输入)。

相反,您的观点可以只是

@using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type = "submit" value="Upload" />
}

并将POST方法更改为

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    if (file.ContentLength > 0) // check a file was selected
    {
       // Initialize a new instance of the data model and set its properties
       ImageGallery model = new ImageGallery()
       {
           FileName = file.FileName,
           ImageSize = file.ContentLength,
           ....
       };
       .... // save and redirect
    }
    else {
      // add a model state error and return the view?
    }
}

答案 2 :(得分:-1)

只需尝试使用此类属性的新视图模型-----

readonly

并改变你的控制器-----

    public class ImageViewModel
   {
    public int ImageID { get; set; }
    public int ImageSize { get; set; }
    public string FileName { get; set; }
    public byte[] ImageData { get; set; }
    public HttpPostedFileBase File { get; set; }
   }

}

并像这样更改您的视图页面------

    public ActionResult Upload(HttpPostedFileBase file)
    {
        ImageGallery IG = new ImageGallery();
    IG.FileName = file.FileName;
      IG.ImageSize = file.ContentLength;

        byte[] data = new byte[file.ContentLength];
        file.InputStream.Read(data, 0, file.ContentLength);

           IG.ImageData = data;

        var model = new ImageViewModel
        {
            FileName = file.FileName,
            ImageSize = file.ContentLength,
            ImageData = data,
            File = file
        };


        using(MyDatabaseEntities dc = new MyDatabaseEntities())
        {
            dc.ImageGalleries.Add(IG);
            dc.SaveChanges();
        }
        return View(model);

    }

}