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

194
Visualizações
How to know if an object is tracked by Entity Framework Core

I'm using .NET 6 and Entity Framework Core 6 (6.0.2) in an ASP.NET Core Web API project.

Assuming I have:

public class MyModel 
{
    public Guid Id { get; set; }
    public string MyData { get; set; }
    //etc...
}

public class ApplicationDbContext : DbContext
{
    public DbSet<MyModel> MyModels { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> opt) : base(opt)
    {
    }
}

If I'm in a generic function with the object MyModel myModel, then how can I know if this instance of MyModel is an entity tracked by Entity Framework Core?

I'm looking for a function like:

ApplicationDbContext context = ...
MyModel myModel = ...
//ecc...
bool result = context.IsOneOfMyEntity(myModel);
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use Entry method

This method may be called on an entity that is not tracked. You can then set the State property on the returned entry to have the context begin tracking the entity in the specified state.

and check the State property for value other than Detached

Detached 0 The entity is not being tracked by the context.

e.g.

bool tracked = context.Entry(myModel).State != EntityState.Detached;
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