Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to upload files in my application. I've used HttpPostedFileBase in my model and a byte[] array but don't know why this error is showing when i'm running my application. Below I've also uploaded the image of the error that is showing when running the app.

The error that is showing is:

One or more validation errors were detected during model generation:

AdSite.Models.HttpPostedFileBase: : 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.`

Error screenshot

My Model:

public class Album
{
    [Key]
    public int Id { get; set; }
    public string ProductTitle { get; set; }
    public string Description { get; set; }
    public string ImageFileName { get; set; }
    public int ImageSize { get; set; }
    public byte[] ImageData { get; set; }
    [Required(ErrorMessage="Please select image file.")]
    public HttpPostedFileBase File { get; set; }        
}

My controller code:

public ActionResult Upload([Bind(Include = "Id,ProductTitle,Description,ImageFileName,ImageData,File,ImageSize")]Album album)
{
    if (ModelState.IsValid)
    {
        //  album.ImageFileName = album.File.FileName;
        // album.ImageSize = album.File.ContentLength;

        byte[] data = new byte[album.File.InputStream.Length];
        album.File.InputStream.Read(data, 0, data.Length);
        album.ImageData = data;

        var db = new AlbumContext();
        db.Albums.Add(album);
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(album);
}

My View code:

<div class="form-group">
    <label class="control-label col-md-2">Select Image:</label> 
    <div class="col-md-10">
        @Html.TextBoxFor(model=>model.File, new { type="file"})
        @Html.ValidationMessage("CustomError")
    </div>
</div>
share|improve this question
    
In the 'Bind' attribute, you define properties for 'album'. Where is the 'File' property coming from? – SRQ Coder Dec 8 '16 at 12:48
    
Hello @SRQCoder , the File property is defined in the Model class. it is of type HttpPostedFileBase File. You can see my model class here. – smk pobon Dec 8 '16 at 12:50
    
You need to remove public HttpPostedFileBase File { get; set; } from your data model (its a complex object and cannot be stored in a database column). You editing data so use a view model and the view model will contain that property (and not the public byte[] ImageData { get; set; } property - What is ViewModel in MVC? – Stephen Muecke Dec 9 '16 at 0:42

This is why you want to use Models to retrieve the data and then convert it to your DTO objects.

The error is because you're trying to store the System.Web.HttpPostedFileBase class into your database. That's not your class to control, and it wasn't created to be stored directly in a database. In this case, HttpPostedFileBase is your "model".

Create another object and tie it into your DbContext to store what you need to about the file in your database. Don't just toss the object in there.

share|improve this answer
    
thanks. but can you please show me an example code of this here? – smk pobon Dec 8 '16 at 13:16
    
do you mean to create a new "model class" for other info about the file that need to upload? that is do i need two class, one for HttpPostedFileBase property and one for the other property?? – smk pobon Dec 8 '16 at 13:20
    
I meant create a DTO class for whatever info you need from HttpPostedFileBase. – krillgar Dec 8 '16 at 13:23
    
I've provided a link to the HttpPostedFileBase class. You already have your Album class, just do a similar process for your File. – krillgar Dec 8 '16 at 13:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.