C
C#14mo ago
caique

❔ 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
No description
14 Replies
Angius
Angius14mo ago
Assuming you have the database already and you generated the EF code, getting data is as simple as
var cpf = await dbContext.Clients
.Where(...)
.Select(client => client.Cpf)
.FirstOrDefaultAsync();
var cpf = await dbContext.Clients
.Where(...)
.Select(client => client.Cpf)
.FirstOrDefaultAsync();
And creating a new schedule/agenda/whatever would just be
dbContext.Agendas.Add(new Agenda {
Name = "",
Cpf = cpf,
Hour = "",
Obs = "",
// ...
});
await dbContext.SaveChangesAsync();
dbContext.Agendas.Add(new Agenda {
Name = "",
Cpf = cpf,
Hour = "",
Obs = "",
// ...
});
await dbContext.SaveChangesAsync();
caique
caiqueOP14mo ago
right, but idk why i cant instance my database context
Angius
Angius14mo ago
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 DI
caique
caiqueOP14mo ago
kinda get it i just dont know why still isn't working
caique
caiqueOP14mo ago
it could work you think?
No description
Angius
Angius14mo ago
You need some actual selector in the .Where() client => client.Id == 1 or some such
Jimmacle
Jimmacle14mo ago
i'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 entity
caique
caiqueOP14mo ago
i 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?
Jimmacle
Jimmacle14mo ago
$code
MODiX
MODiX14mo ago
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/
Jimmacle
Jimmacle14mo ago
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
MODiX
MODiX14mo ago
caique
it could work you think?
From caique
React with ❌ to remove this embed.
caique
caiqueOP14mo ago
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
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server