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

asp.net asp.net-core-webapi asp.net-web-api entity-framework-core repository-pattern

Question

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.

1
1
2/26/2018 9:10:19 AM

Accepted Answer

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.

1
2/26/2018 8:41:19 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow