neo
neo

Reputation: 75

'HttpPostedFileBase' has no key defined

I have a web application to upload image to database and to retreive them.

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; }
}

and my database context class is something like this

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

and here is my controller

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");
}

now when i try to upload the image it giving me following error

EntityType 'HttpPostedFileBase' has no key defined. Define the key for this EntityType. HttpPostedFileBases: EntityType: EntitySet 'HttpPostedFileBases' is based on type 'HttpPostedFileBase' that has no keys defined.

I have seen one of question on stack overflow-----'HttpPostedFileBase' has no key defined. Define the key for this EntityType. I tried the solution but did not get any success.

I was following this blog for this purpose------ http://dotnetawesome.com/mvc/how-to-upload-image-to-database-and-show-in-view-without-image-handler

Upvotes: 0

Views: 2976

Answers (3)

Alfred Jose
Alfred Jose

Reputation: 91

Try this

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

This will not map to the database.

The error occurs because their is no datatype HttpPostedFileBase in your table

Upvotes: 4

neo
neo

Reputation: 164

Simply try a new view model with properties like this-----

    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; }
   }

and change your controller like this-----

    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);

    }

}

}

and change your view page like this------

    @model Image.Models.ImageViewModel

<h2>Upload</h2>
 @using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
 {
  @Html.ValidationSummary(true)
  <table>
    <tr>
        <td>Select File : </td>
        <td>
            @Html.TextBoxFor(Model => Model.File, new { type = "file" })
            @Html.ValidationMessage("CustomError")
        </td>
       <td>
            <input type="submit" value="Upload" />
        </td>
    </tr>
</table>
 }

Upvotes: -1

user3559349
user3559349

Reputation:

You cannot store HttpPostedFileBase in a database field (its a complex object containing multiple properties). You could exclude this using the [NotMapped] attribute, however your model and your view really have no relationship (your not including any inputs for the other properties of your model).

Instead your view can be just

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

and change the POST method to

[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?
    }
}

Upvotes: 1

Related Questions