0

I am making a Web API with .Net and it is receiving a custom JSON object from a web form to serialize to the model. I am currently having an issue displaying the API response. I am storing the data in an in memory database at first just to get it working.

I am not sure why it is asking me for a primary key despite there being a key defined on my model.

My next step is to add multipart-form data but that is its own can of worms. The error happens on the _context.ProjectItems.ToListAsync(); line. I have already added an ID to the model.

    [Required]
    [Key]
    public new long Id { get; set; }

    [HttpGet("{id}")]
    public async Task<ActionResult<ProjectItem>> GetProjectItem(long id)
    {
        var projectItem = await _context.ProjectItems.FindAsync(id);

    if (projectItem == null)
    {
        return NotFound();
    }



    return projectItem;



   }
  [HttpGet]
    public async Task<ActionResult<IEnumerable<ProjectItem>>> 
GetProjectItems()
    {
        return await _context.ProjectItems.ToListAsync();
    }

My model: ProjectItem.cs

    using System.ComponentModel.DataAnnotations;

    namespace RupAPI.Controllers
    {
        public class ProjectItem : Project
        {
            [Required]
            [Key]
            public new long Id { get; set; }

            public string ProjName { get; set; }
            public DateTime DateSub { get; set; }
            public DateTime DueDate { get; set; }
            public DateTime ArrivalDate { get; set; }
            public string Desc { get; set; }
            public Array Sku { get; set; }
            public Array SkuDesc { get; set; }
            public Array Item { get; set; }
            public Array ItemDesc { get; set; }
            public Array FileName { get; set; }
            public new byte[] File { get; set; }
        }
    }

An unhandled exception occurred while processing the request. InvalidOperationException: The entity type 'Array' requires a primary key to be defined. Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model) Stack Query Cookies Headers InvalidOperationException: The entity type 'Array' requires a primary key to be defined. + return await _context.ProjectItems.ToListAsync(); lambda_method(Closure , object )

Dharman
  • 30,962
  • 25
  • 85
  • 135
JhoD668
  • 3
  • 3

1 Answers1

0

Reconsider the usage of Array as type for your properties, which cause this issue.

If those properties are really more than just string, create additional model(s) to represent the properties and use relationships to store them in your database.

If the properties are lists of strings, there are several solutions proposed here. If possible I would go for Sasan's solution, which is for your case to replace Array with string[] and add

modelBuilder.Entity<ProjectItem>()
    .Property(e => e.Sku)
    .HasConversion(
        v => string.Join(',', v),
        v => v.Split(',', StringSplitOptions.RemoveEmptyEntries));
// TODO: also to that for the other properties

to your database context's OnModelCreating.

user7217806
  • 2,014
  • 2
  • 10
  • 12
  • Ok, I see that Array causes the problem. Can you elaborate on the solution you offered or give any code examples? My JSON object is passing arrays of strings so I am at a loss with how to bind these to properties if I am not using the datatype Array. – JhoD668 Aug 27 '19 at 20:09
  • Thank you for the quick response! I am not sure that is the direction I want to go with this. I upvoted your answer but it won't show cause I have no feedback score myself. I will see if anyone has an alternative. Thanks again. – JhoD668 Aug 27 '19 at 20:31
  • You are welcome! Alternatives would be to either serialize the string lists and store the result as string, or store the data in separate tables (thus models and relations). – user7217806 Aug 27 '19 at 20:37