0

demonestrationOne of my questions is closed due to insufficient information so I written another with detail.

I want to write a blazor app in c# & .netCore plateform using Visual Studio 2022.

Such app will be operated as Inventory/Financial Management System, CRM, HRM etc. so this system is based on more than 30 tables where the date will be saved via CRUD operation.

Here the question is how I can implement single CRUD for all these tables like same UpdateTable function (with parameters) will be usable to update the tables like Customer, Supplier, Products etc in my database.

something like below..

function DbSet UpdateTable(DbSet tableName)
 {
   _context.Update(tableName);
 }

Actually this will help me save my time to write several lines of code /(I mean writing CRUD) for each and every table

thanks in advance

1

1 Answer 1

1

It looks you are looking for Entity Framework Core Generic Repository:

The advantage of having generic CRUD repository is that you can inherit from it, pass it entity type and you have CRUD repository for any entity type with a minimal amount of code.

public interface IGenericRepository<TEntity>
 where TEntity : class, IEntity
{
    IQueryable<TEntity> GetAll();

    Task<TEntity> GetById(int id);

    Task Create(TEntity entity);

    Task Update(int id, TEntity entity);

    Task Delete(int id);
}


public class GenericRepository<TEntity> : IGenericRepository<TEntity>
    where TEntity : class, IEntity
{
    private readonly CodingBlastDbContext _dbContext;

    public GenericRepository(CodingBlastDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    // ... Other CRUD ops

    public async Task Update(int id, TEntity entity)
    {
        _dbContext.Set<TEntity>().Update(entity);
        await _dbContext.SaveChangesAsync();
    }

}

Read the blog to learn how to create the generic repository. But, be carefully. Because you are using Blazor, you should deal with Blazor A second operation started on this context before a previous operation completed

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.