1

I am trying to upload images to the server and save them so that I can use them on user site but I am getting this mapping error and need help to solve this.

I tried changing the property name from string to IFormFile but that didn't work. Then I changed it again to string and created another property

public IFormFile Thumbnail 

but I get an ambiguity error to solve this I changed the property to

public IFormFile Thumbnailimg 

and same for the Bug and ShortVideo.

Model.cs

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;

namespace BuildShow.Models
{
    public partial class Assets
    {
        public int Id { get; set; }
        public int TypeFid { get; set; }
        public int CategoryFid { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string ShortDescription { get; set; }
        public string LongDescription { get; set; }
        public string ArticleText { get; set; }
        public string Thumbnail { get; set; }
        public string Hero { get; set; }
        public string Bug { get; set; }
        public string ShortVideo { get; set; }
        public string VideoId { get; set; }
        public int TagFid { get; set; }
        public int ContributerFid { get; set; }
        public bool? Enabled { get; set; }
        public bool? Featured { get; set; }
        public bool? ShowOnHomePage { get; set; }
        public string SortOrder { get; set; }

        public virtual Categories CategoryF { get; set; }
        public virtual Contributor ContributerF { get; set; }
        public virtual Tags TagF { get; set; }
        public virtual AssetType TypeF { get; set; }


        public IFormFile Thumbnailimg { get; set; }
        public IFormFile Heroimg { get; set; }
        public IFormFile Bugimg { get; set; }
        public IFormFile ShortVideoup { get; set; }
    }
}

Controller.cs

if (id != assets.Id)
{
    return NotFound();
}

if (ModelState.IsValid)
{
    try
    {
        _context.Update(assets);
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!AssetsExists(assets.Id))
        {
            return NotFound();
        }
        else
        {
            throw;
        }
    }

    return RedirectToAction(nameof(Index));
}

Error:

InvalidOperationException: The property 'Assets.Thumbnailimg' is of an interface type ('IFormFile'). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type, otherwise ignore the property using the NotMappedAttribute or 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Honor
  • 15
  • 1
  • 4
  • 1
    As indicated in the error, `IFormFile` is just an interface to get metadata from the posted file, you can not directly save this to database. It is a design decision what to do with posted file (IFormFile). This [post](https://stackoverflow.com/questions/42741170/how-to-save-images-to-database-using-asp-net-core) might help you. – ibubi Oct 31 '19 at 20:30

1 Answers1

0

In order to save to your databse your model needs to reflect your table.

You cannot save to your database an interface type

If you do not want to use this property in you db you can add:

[NotMapped]
David Edel
  • 569
  • 4
  • 18