代码之家  ›  专栏  ›  技术社区  ›  Aline Raphaela

“达多斯库”酒店。无法映射CnpjFornecedor,因为它的类型为“List<string>”

  •  0
  • Aline Raphaela  · 技术社区  · 2 年前

    我正在尝试从模型上的属性进行迁移,并返回以下消息:

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

    我的代码:

    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>();
        }
    }
    

    我不明白问题出在哪里。要迁移属性,我应该更改什么?

    1 回复  |  直到 2 年前
        1
  •  0
  •   Reza Heidari    2 年前

    你试图以错误的方式建立一对多的关系。您可以尝试以下方法:

    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; }
    }
    

    现在,您与导航属性有了正确的一对多关系。