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'.

1
  • 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 might help you. Commented Oct 31, 2019 at 20:30

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

I want to save the address of file in database
"I tried changing the property name from string to IFormFile but that didn't work." Why did you do this?
I can't figure out how to do it
Are you trying to upload images to the server than save the URL?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.