【问题标题】:Relationship in EF Core6EF Core 6 中的关系
【发布时间】:2021-11-13 02:22:42
【问题描述】:

我只是在测试 Net 6,在一个测试 Blazor 应用程序中我有以下类,我将它与 EF Core 6 一对多关联,但是当我想执行 POST 以保存在数据库中时,它要求我包含外键中的模型,在 Net Core 3.1 中执行一切正常,但在 Net 6 中出现以下错误,我是否在 EF6 中遗漏了什么?

模型

 public class DatosPersonales
    {
        public int Id { get; set; }
        public string UserId { get; set; }
        public DateTimeOffset FechaRegistro { get; set; }
        [Required]
        public string Nombre { get; set; }
        [Required]
        public string PrimerApellido { get; set; }
        public string? SegundoApellido { get; set; }
        [Required]
        public string RFC { get; set; }
        [Required]
        public string Homoclave { get; set; }
        [Required]
        public string CURP { get; set; }
        [Range(1, int.MaxValue)]
        public int PaisNacimientoId { get; set; }
        public CatPais PaisNacimiento { get; set; }
    }



public class CatPais
    {
        public int Id { get; set; }
        [Required]
        public string Pais { get; set; }
        [Required]
        public string Codigo { get; set; }
        public ICollection<DatosPersonales> DatosPersonales { get; set; }
    }

控制器

 public async Task<ActionResult<int>> Post(DatosPersonales datosPersonales)
        {
            context.DatosPersonales.Add(datosPersonales);
            await context.SaveChangesAsync();
            return datosPersonales.Id;
        }

JSON POST

{"Id":0,"UserId":"613544e8-ff4f-40cc-9389-10fcd8aa5b35","FechaRegistro":"0001-01-01T00:00:00+00:00","Nombre":"Jhon","PrimerApellido":"Brown","SegundoApellido":null,"RFC":"BRJO56GT19","Homoclave":"ER2","CURP":"BRJO56GT19HYT4521E","PaisNacimientoId":1}

POST 的结果

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-58d58c01790a68cfb7f75c94f5bb637d-8f8db8b15825726d-00",
    "errors": {
        "PaisNacimiento": [
            "The PaisNacimiento field is required."
        ]
    }
}

希望有人能帮助我

【问题讨论】:

    标签: c# entity-framework .net-6.0


    【解决方案1】:

    我已经通过添加允许属性为空来修复它

    public class DatosPersonales
        {
            public int Id { get; set; }
            public string UserId { get; set; }
            public DateTimeOffset FechaRegistro { get; set; }
            [Required]
            public string Nombre { get; set; }
            [Required]
            public string PrimerApellido { get; set; }
            public string? SegundoApellido { get; set; }
            [Required]
            public string RFC { get; set; }
            [Required]
            public string Homoclave { get; set; }
            [Required]
            public string CURP { get; set; }
            [Range(1, int.MaxValue)]
            public int PaisNacimientoId { get; set; }
            public CatPais? PaisNacimiento { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-20
      • 2022-10-17
      • 1970-01-01
      • 2021-11-25
      • 2021-05-01
      • 2022-11-02
      • 2023-02-10
      • 1970-01-01
      相关资源
      最近更新 更多