Friends, it had a "person" table where it records the user's information. Now I created an "address" table to store the user's address information.
Now I need to reorganize my code because I'm having relationship problems. These are my files:
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; }
}
}
Address.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; }
}
}
Here on this line, I get an error that I can't solve:
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();}
I need to access the fields of the address table, through the person table. I believe that's it, but I get the error:
MySql.Data.MySqlClient.MySqlException: 'Unknown column 'a.addresscod_address' in 'field list''
I also tried: a.person.address, but the same error occurs.
I appreciate if anyone can analyze.