Generic CRUD in Entity Framework with unspecified entity

Asked

Viewed 160 times

1

I’m trying to make a generic CRUD for my project. However as I used Databasefirst I do not see how to have a generic class of Entity that can be inherited. Because it doesn’t make any sense, eventually when I upgrade the bank, I would have to go into all over 60 classes of the tables and add the inheritance again. I want the entity classes that Entity Framawork generated pure, such as were generated.

So I’m trying something like: inserir a descrição da imagem aqui

But as you can see I have this property problem not defined, since as I said I don’t want to have anything like a "Entidadegeral".

Does anyone know of any way to achieve this? Like has some class that the Entity uses behind the scenes that can be used in the constraint Where. Or maybe if instead of Generics I used Reflection? Any ideas?

Edited: Code Added.

public class DaoEF<TEntity> : IDaoEF<TEntity>
    where TEntity : class
{
    public GPSdEntities _dbContext { get; set; } = new GPSdEntities();


    public async Task<TEntity> GetById(int id)
    {
        return await _dbContext.Set<TEntity>()
                    .AsNoTracking()
                    .FirstOrDefaultAsync(e => e.Id == id);
    }
  • publish the code and not an image of it, get more dynamic help.

  • Code added... Wanted to show error, so image.

1 answer

3


First, CRUD generic the Entity Framework already possesses, the same is called DbSet<T>.

Second point, I see that you want to abuse the AsNoTracking, this will bring you more problems than gain, so I advise you not to encapsulate it, let the developer decide when to use it.

Third point, if all you need is to write reusable queries, then extend the DbContext. The way you are doing, you will have multiple Contexts working in the same working unit.

public static class EFExtensions<TEntity> where TEntity : class
{
    public static async Task<TEntity> GetById<TEntity>(this GPSdEntities context, int id)
    {
        return await _dbContext.Set<TEntity>()
            .AsNoTracking()
            .FirstOrDefaultAsync(e => e.Id == id);
    }
}

Now answering your question, the automatically generated classes are all partial, so let’s say you have a class like the following.:

public partial class MinhaClass
{
    public string Prop1 { get; set; }
}

you can define in a second file the complement of this class.:

public partial class MinhaClass : IDisposable
{
    public void Dispose()
    { 

    }
}

even you would already have to create this file/class if you need to add some attribute to the generated properties.:

public class MinhaClassMeta
{
    [Display(Name = "Propriedade 01")]
    public string Prop1 { get; set; }
}


[MetadataType(typeof(MinhaClassMeta))]
public partial class MinhaClass : IDisposable
{
    public void Dispose()
    { 

    }
}
  • So Tobias... Thank you so much for the answer. It was accurate and complete. Just so you understand, this generic class would aim to be inherited by the specific classes of each table in a persistence layer, centralizing the common code. Valew!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.