❔ trouble with data with PostgreSQL and EFCore
I'm doing this project with clients data, i have to get the CPF (type of code) from a client that has already a register in my db and insert a scheduling with the client's code*
here is the problem, idk how to get this data when i call the client, is there any simple way i could do that?
i have 2 tables: tb_clientes and tb_agendamentos
14 Replies
Assuming you have the database already and you generated the EF code, getting data is as simple as
And creating a new schedule/agenda/whatever would just be
right, but idk why i cant instance my database context
Why would you instance it?
Ah, the context?
¯\_(ツ)_/¯
You should be able to
using var ctx = new MyCoolDbContext()
for example
Ideally, though, you'd have it injected with DIkinda get it
i just dont know why still isn't working
it could work you think?
You need some actual selector in the
.Where()
client => client.Id == 1
or some suchi'm confused, why do you have a dbcontext and then these two other
Tb...
types?
what do those represent? i would assume tables based on the prefix but then the way you assign them makes me think they represent a single entityi dont know actually, i'll bring the code
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace Orlagenda;
public partial class OrlagendaDatabaseContext : DbContext
{
public OrlagendaDatabaseContext()
{
}
public OrlagendaDatabaseContext(DbContextOptions<OrlagendaDatabaseContext> options)
: base(options)
{
}
public virtual DbSet<TbAgendamento> TbAgendamentos { get; set; }
public virtual DbSet<TbCliente> TbClientes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseNpgsql("Server=192.168.0.131; Port=5432; Database=orlagenda_database; UserID=postgres; Password=457320318");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TbCliente>(entity =>
{
entity.HasKey(e => e.CodigoCliente).HasName("tb_clientes_pkey");
entity.ToTable("tb_clientes");
entity.Property(e => e.CodigoCliente).HasColumnName("codigo_cliente");
entity.Property(e => e.CidadeCliente)
.HasMaxLength(25)
.HasColumnName("cidade_cliente");
entity.Property(e => e.CpfCliente)
.HasMaxLength(14)
.HasColumnName("cpf_cliente");
entity.Property(e => e.DatanascCliente)
.HasMaxLength(14)
.HasColumnName("datanasc_cliente");
entity.Property(e => e.EnderecoCliente)
.HasMaxLength(80)
.HasColumnName("endereco_cliente");
entity.Property(e => e.GeneroCliente)
.HasMaxLength(10)
.HasColumnName("genero_cliente");
entity.Property(e => e.NomeCliente)
.HasMaxLength(60)
.HasColumnName("nome_cliente");
});
modelBuilder.Entity<TbAgendamento>(entity =>
{
entity.HasKey(e => e.CodigoAgendamento).HasName("tb_agendamentos_pkey");
entity.ToTable("tb_agendamentos");
entity.Property(e => e.CodigoAgendamento).HasColumnName("codigo_agendamento");
entity.Property(e => e.CpfcliAgendamento)
.HasMaxLength(14)
.HasColumnName("cpfcli_agendamento");
entity.Property(e => e.DataAgendamento)
.HasMaxLength(11)
.HasColumnName("data_agendamento");
entity.Property(e => e.HoraAgendamento)
.HasMaxLength(5)
.HasColumnName("hora_agendamento");
entity.Property(e => e.NomecliAgendamento)
.HasMaxLength(60)
.HasColumnName("nomecli_agendamento");
entity.Property(e => e.ObsAgendamento)
.HasMaxLength(200)
.HasColumnName("obs_agendamento");
entity.Property(e => e.CodigoClienteAgendamento)
.HasColumnName("codigocli_agendamento");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
how do i put on code block btw?
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/in your code you should be making queries off your dbcontext, not your entities
so this doesn't really make sense to me https://discord.com/channels/143867839282020352/1164644260562341948/1164649376560447628
caique
it could work you think?
Quoted by
<@901546879530172498> from #trouble with data with PostgreSQL and EFCore (click here)
React with ❌ to remove this embed.
i couldnt by my dbcontext, and i dont know why either
may be my approach with EFCore?
because i used DBFirst
idk if it is different
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.