未知列';a、 地址cod#u地址';在';字段列表-C#
朋友们,它有一个“person”表,记录用户的信息。现在我创建了一个“地址”表来存储用户的地址信息 现在我需要重新组织我的代码,因为我有关系问题。这些是我的文件: Person.cs未知列';a、 地址cod#u地址';在';字段列表-C#,c#,mysql,asp.net-mvc,C#,Mysql,Asp.net Mvc,朋友们,它有一个“person”表,记录用户的信息。现在我创建了一个“地址”表来存储用户的地址信息 现在我需要重新组织我的代码,因为我有关系问题。这些是我的文件: Person.cs using LojaVirtual.Libraries.Lang; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.Dat
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; }
}
}
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; }
}
}
使用LojaVirtual.Models;
使用LojaVirtual.Models.ProdutoAgregador;
使用Pomelo.EntityFrameworkCore.MySql;
使用Microsoft.EntityFrameworkCore;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.EntityFrameworkCore;
名称空间LojaVirtual.Database
{
公共类LojaVirtualContext:DbContext
{
/*
*EF核心-ORM
*ORM->与护墙板相关的地图库
*/
公共LojaVirtualContext(DbContextOptions选项):基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasOne(s=>s.address)
.有一个(广告=>广告人)
.HasForeignKey(ad=>ad.cod_人);
}
公共DbSet person{get;set;}
公共数据库集顺序{get;set;}
公共数据库集地址{get;set;}
}
}
在这一行,我得到了一个我无法解决的错误:
public List ObterTodosPedidosPorSituacao(字符串状态)
{
返回银行订单
.包括(a=>a.订单情况)
.包括(a=>a.person)
.然后包括(p=>p.address)
.Where(a=>a.Situation==status).ToList();}
我需要通过person表访问address表的字段。我相信是这样,但我得到了一个错误:
MySql.Data.MySqlClient.MySqlException:“字段列表”中的未知列“a.addresscod\u address”
我还尝试了:a.person.address,但出现了相同的错误
如果有人能分析,我将不胜感激。这应该可以:
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();
}
public List ObterTodosPedidosPorSituacao(字符串状态)
{
返回银行订单
.包括(a=>a.订单情况)
.包括(a=>a.person)
.然后包括(p=>p.address)
其中(a=>a.Situation==status).ToList();
}
但我只是猜测,因为并不是所有的代码都包含在内。如果我能提出一些建议的话——在c#中存在严格的限制。这没什么大不了的,但它有助于团队合作:)
编辑:我看到了数据注释的其他问题<代码>[ForeignKey(“cod_person”)]-这是多余的。请记住在更改后执行数据库迁移。这应该可以:
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();
}
public List ObterTodosPedidosPorSituacao(字符串状态)
{
返回银行订单
.包括(a=>a.订单情况)
.包括(a=>a.person)
.然后包括(p=>p.address)
其中(a=>a.Situation==status).ToList();
}
但我只是猜测,因为并不是所有的代码都包含在内。如果我能提出一些建议的话——在c#中存在严格的限制。这没什么大不了的,但它有助于团队合作:)
编辑:我看到了数据注释的其他问题<代码>[ForeignKey(“cod_person”)]-这是多余的。请记住在更改后执行数据库迁移。此处存在关系配置错误
modelBuilder.Entity<person>()
.HasOne<address>(s => s.address)
.WithOne(ad => ad.person)
.HasForeignKey<person>(ad => ad.cod_person); // <-- problem: ad is person
还考虑删除(并且一般避免)<代码>外键属性。它们具有不同的语义,具体取决于应用的位置(在导航属性上指定关联的FK属性名称,在FK属性上指定关联的导航属性名称),并且很容易出错,如您的情况:
[ForeignKey("cod_person")] // <-- wrong, points to self
public int? cod_person { get; set; }
public person person { get; set; }
或
但正如我前面所说的,最好避免使用该属性,并在需要时使用fluent API配置关系。这里存在关系配置错误
modelBuilder.Entity<person>()
.HasOne<address>(s => s.address)
.WithOne(ad => ad.person)
.HasForeignKey<person>(ad => ad.cod_person); // <-- problem: ad is person
还考虑删除(并且一般避免)<代码>外键属性。它们具有不同的语义,具体取决于应用的位置(在导航属性上指定关联的FK属性名称,在FK属性上指定关联的导航属性名称),并且很容易出错,如您的情况:
[ForeignKey("cod_person")] // <-- wrong, points to self
public int? cod_person { get; set; }
public person person { get; set; }
或
但正如我前面所说,最好避免使用该属性,并在需要时配置与fluent API的关系。什么是银行?你能提供完整的代码,包括构造函数吗?什么是银行?你能提供包括构造器在内的完整代码吗?谢谢你的反馈。我更新了密码,收到以下内容:MySql.Data.MySqlClient.MySqlException:“字段列表”中的未知列“a.addresscod_address”现在我得到了这个结果:“无法将属性或导航“person”添加到实体类型“address”,因为实体类型“address”上已经存在同名的属性或导航。”。我更新了密码,收到以下内容:MySql.Data.MySqlClient.MySqlException:“字段列表”中的未知列“a.addresscod_address”现在我得到了这个结果:“无法将属性或导航“person”添加到实体类型“address”,因为实体类型“address”上已经存在同名的属性或导航。”。我更新了密码,收到以下内容:MySql.Data.MySqlClient.MySqlException:“字段列表”中的未知列“a.addresscod_address”现在我得到了这个结果:“无法将属性或导航“person”添加到实体类型“address”,因为实体类型“address”上已经存在同名的属性或导航。谢谢您的反馈。我更新了代码,收到了以下内容:MySql.Data.MySqlClient.MySqlException:“字段列表”中的未知列“a.addresscod_address”现在我得到了这个结果:“无法将属性或导航“person”添加到实体类型“address”,因为实体类型“address”上已经存在同名的属性或导航
public int? cod_person { get; set; }
[ForeignKey("cod_person")] // the name of the above FK property
public person person { get; set; }