Tahjid Ashfaque
Tahjid Ashfaque

Reputation: 855

How to implement Repository pattern for for handling Data in Asp.Net Core Web API project without Adding multiple Dbset lines for multiple Tables

I am trying to implement repository pattern in one of my personal projects. I am doing it with Asp.Net Core Web Api using Code First Approach. I am a complete beginner in this. So I apologize in advance if my question seems ignorant.

My question is, for every table in my database, I have to add

public DbSet<tableName> TableName{ get; set; }

in my DbContext, which does not seem like the correct approach. I was wondering if there was a generic way of doing this. I tried having a base class and all of my other data models inherit from that class. But this just creates a table for the baseclass. I could not find an answer to this anywhere. Is this possible? I don't want to have to edit db context every time I create a new Table. If this is possible please kindly guide me or provide a link to any tutorials.

Thanks in advance.

Upvotes: 1

Views: 276

Answers (1)

garret
garret

Reputation: 1134

Adding DbSet for every table is correct approach.

it's not to many code to add multiple lines of

public DbSet<ClassName> TableName { get; set; }

Your DbContext have to "know" every table which it has to create. You don't have any other way to inform dbContext what to create.

Upvotes: 1

Related Questions