Home > Enterprise >  The property 'DadosRequisicao.CnpjFornecedor' could not be mapped because it is of type &#
The property 'DadosRequisicao.CnpjFornecedor' could not be mapped because it is of type &#

Time:04-27

I'm trying to do the migration from my properties on Models, and is returning this message:

The property 'DadosRequisicao.CnpjFornecedor' could not be mapped because it is of type 'List<string>', which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

My code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace PortalDeCotacao.Models
{
    public class DadosRequisicao
    {
        [Key]
        [Required]
        public int Id { get; set; }
        public string DataLimiteCot { get; set; }
        public string ObservacoesReq { get; set; }
        public List<string> CnpjFornecedor { get; set; } = new List<string>();
        public List<int> CodProduto { get; set; } = new List<int>();
    }
}

I didn't understand where is the problem. what should I change to migrate the properties?

CodePudding user response:

You are trying to generate a One-To-Many relationship in the wrong way. You can try the following approach:

public class DadosRequisicao
{
    [Key]
    [Required]
    public int Id { get; set; }
    public string DataLimiteCot { get; set; }
    public string ObservacoesReq { get; set; }
    public List<CnpjFornecedor> CnpjFornecedor { get; set; } = new();
    public List<CodProduto> CodProduto { get; set; } = new();
}

public class CnpjFornecedor
{
    public int Id { get; set; }
    public DadosRequisicao DadosRequisicao { get; set; }
    public string Data { get; set; }
}

public class CodProduto
{
    public int Id { get; set; }
    public DadosRequisicao DadosRequisicao { get; set; }
    public int Data { get; set; }
}

Now you have a correct One-To-Many relationship with navigation property.

  • Related