【问题标题】:Unknown column 'a.addresscod_address' in 'field list - C#'字段列表中的未知列'a.addresscod_address' - C#
【发布时间】:2021-03-30 03:14:02
【问题描述】:

朋友们,它有一个记录用户信息的“人”表。现在我创建了一个“地址”表来存储用户的地址信息。

现在我需要重新组织我的代码,因为我遇到了关系问题。这些是我的文件:

Person.cs

using LojaVirtual.Libraries.Lang;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace LojaVirtual.Models
{
    public class person
    {
        /* PK */
        [Key]
        public int cod_person { get; set; }

        public string name { get; set; }
        public DateTime date { get; set; }
        public string gender{ get; set; }
        .
        .
        public string email { get; set; }
        public string password { get; set; }
        public addres addres { get; set; }
        
        [ForeignKey("cod_person")]
        public virtual ICollection<order> Order { get; set; }
    }
}

地址.cs

using System;
using System.Collections.Generic;
using LojaVirtual.Libraries.Lang;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace LojaVirtual.Models
{
    public class address
    {
        [Key]
        public int cod_address{ get; set; }

        public string { get; set; }
        public string neighborhood{ get; set; }

        public string city { get; set; }
        public string state{ get; set; }
        .
        .

        [ForeignKey("person")]
        public int? cod_person { get; set; }

        public person person { get; set; }
    }
}

LojaVirtualContext.cs


using LojaVirtual.Models;
using LojaVirtual.Models.ProdutoAgregador;
using Pomelo.EntityFrameworkCore.MySql;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;


namespace LojaVirtual.Database
{
    public class LojaVirtualContext : DbContext
    {
        /*
         * EF Core - ORM
         * ORM -> Bibliteca mapear Objetos para Banco de Dados Relacionais
         */
        public LojaVirtualContext(DbContextOptions<LojaVirtualContext> options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<person>()
                .HasOne<address>(s => s.address)
                .WithOne(ad => ad.person)
                .HasForeignKey<address>(ad => ad.cod_person);

        }
        public DbSet<person> person { get; set; }
        public DbSet<order> order{ get; set; }
        public DbSet<address> addres { get; set; }
    }
}

在这一行,我得到一个我无法解决的错误:

public List<order> ObterTodosPedidosPorSituacao(string status)
{
 return _banco.order
       .Include(a => a.order_situation)
       .Include(a => a.person)
       .ThenInclude(p => p.address)
       .Where(a => a.Situation == status).ToList();}

我需要通过 person 表访问地址表的字段。我相信就是这样,但我得到了错误:

MySql.Data.MySqlClient.MySqlException: '字段列表'中的未知列'a.addresscod_address''

我也试过:a.person.address,但出现同样的错误。

如果有人可以分析,我很感激。

【问题讨论】:

  • 什么是_banco?能否请您包括完整的代码,包括构造函数?

标签: c# mysql asp.net-mvc


【解决方案1】:

这里有关系配置错误

modelBuilder.Entity<person>()
    .HasOne<address>(s => s.address)
    .WithOne(ad => ad.person)
    .HasForeignKey<person>(ad => ad.cod_person); // <-- problem: ad is person

最后一行ad 的类型是HasForeignKey 泛型类型参数的类型。你应该这样读——HasForeignKey 的泛型类型参数是包含外键的实体类型。

所以你在这里所做的实质上是配置person 实体的PK 以也用作address 的FK,这会使address 中的预期FK 无效(因为流畅的配置具有更高的优先级/覆盖约定和数据注释)。

话虽如此,只需通过更改泛型类型参数将address 映射为一对一关系的依赖实体(具有 FK 的实体):

.HasForeignKey<address>(ad => ad.cod_person);

还可以考虑删除(并通常避免)ForeignKey 属性。它们具有不同的语义,具体取决于应用的位置(在导航属性上指定关联的 FK 属性名称,在 FK 属性上它们指定关联的导航属性名称),并且很容易像您的情况一样搞砸:

[ForeignKey("cod_person")] // <-- wrong, points to self
public int? cod_person { get; set; }
public person person { get; set; }

应该是

[ForeignKey("person")] // the name of the below navigation property
public int? cod_person { get; set; }
public person person { get; set; }

public int? cod_person { get; set; }
[ForeignKey("cod_person")] // the name of the above FK property
public person person { get; set; }

但正如我之前所说,最好避免使用该属性并在需要时配置与流式 API 的关系。

【讨论】:

  • 感谢您的反馈。我更新了代码,收到以下信息:MySql.Data.MySqlClient.MySqlException: '字段列表'中的'未知列'a.addresscod_address'
  • 现在我得到了这个结果:“无法将属性或导航“人”添加到实体类型“地址”,因为实体类型“地址”上已经存在同名的属性或导航
【解决方案2】:

这应该可行:

public List<order> ObterTodosPedidosPorSituacao(string status)
{
    return _banco.order
       .Include(a => a.order_situation)
       .Include(a => a.person)
       .ThenInclude(p => p.address)
       .Where(a => a.Situation == status).ToList();
}

但我只是猜测,因为并非所有代码都包含在内。如果我可以提出一些建议 - 在 c# 中存在严格的naming convention。这没什么大不了的,但它有助于团队合作:)。

编辑:我看到数据注释的其他问题。 [ForeignKey("cod_person")] - 这是多余的。更改后记得执行数据库迁移。

【讨论】:

  • 感谢您的反馈。我更新了代码,收到以下信息:MySql.Data.MySqlClient.MySqlException: '字段列表'中的'未知列'a.addresscod_address'
  • 现在我得到了这个结果:“无法将属性或导航“人”添加到实体类型“地址”,因为实体类型“地址”上已经存在同名的属性或导航'.'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2023-03-14
  • 2020-10-27
  • 2017-09-03
  • 2011-07-27
相关资源
最近更新 更多