Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
Proxy Items in Entity Framwork Core

Is it possible to create as a proxy element in EF Core?

For example, in the database there is the element with the id 1, which has the name Example. The second element with id 2 has no name (is null), but has a reference to element 1 ("id_replace"). In this case I would like the name returned by item 2 to be "Example" like item 1. And also the "Includes" quote to item 1 references.

The reason I have such a strange idea is that I need to have linked the elements, and if element 1 changes, the changes made are displayed on element 2 as well.

Example Registers in Database

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Sure you can. Assuming that your class is:

public class YourClass
{
    public int id { get; set; }
    public string name { get; set; }
    public int? id_replace { get; set; }
}

In your class, you need to have the one to many referencing properties:

    public YourClass parent { get; set; }
    public IList<YourClass> children { get; set; }

Then, in your DbContext class, in the override OnModelCreating function, you need to have a relationship set in the fluent API that indicates that id_replace is a self-referencing foreign key:

modelBuilder.Entity<YourClass>(entity =>
{
     entity.HasOne(x => x.parent)
         .WithMany(x => x.children)
         .HasForeignKey(x => x.id_replace)
         .OnDelete(DeleteBehavior.SetNull);
});

After doing that(and migrating), you have the necessary navigation properties to be able to add computed properties that do not represent anything in the database. So your class can have the property:

    public int alt_name => name??$"\"{parent.name}\"";

So eventually, your class will look something like this:

public class YourClass
{
    public int id { get; set; }
    public string name { get; set; }
    public int? id_replace { get; set; }
    public YourClass parent { get; set; }
    public IList<YourClass> children { get; set; }
    public int alt_name => name??$"\"{parent.name}\"";
}

That way, you can discard the name property and just call on the alt_name property. You can even set the name property as private or change the names to avoid confusion.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda