0

I am using EF6 but would be calling too many SPs in my project.

I have already put the methods in MyDbContext.cs but since there are too many SP methods, I don't want to crowd the MyDbContext.cs. I want to place the methods in another file. Is there any alternative?

Any good example or article?

Nands
  • 379
  • 3
  • 19

1 Answers1

1

It seems like you are choosing between using Stored Procedure or ORM (in this case the EF6).

In that case, you can read this good article that may help you to decide :

Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures?

EDIT :

As far as I know, using Stored Procedures in Entity Framework is not the best practice, and it will only reduce the purpose why you choose the ORM in the first place.

However, if you insist, you may find this article quite comprehensively covering the topic you are talking about :

http://www.entityframeworktutorial.net/entityframework6/dbcontext.aspx

In short :

  1. Yes, put all the SP callings in the DbContext
  2. If possible, avoid SqlDataReader when you're using EF , or you will end up messing it up again with ADO.NET.

EDIT :

ADDITIONAL TIPS :

I just realized that you have to deal with so many SPs that can potentially make your DbContext class too crowded to manage. In that case, just use PARTIAL CLASS to organize many SPs into separate files, so you can manage them all a lot more easier.

  • No. I am calling SPs and then using IObjectContextAdapter to map SqlDataReader object to Model object. I am doing this in method: " public List dsp_GetDetails() " My question is should I place this method in MyDbContext.cs ? – Nands Feb 28 '18 at 11:28
  • I've just edited my answer to cover your needs this time (hopefully). – Erlangga Hasto Handoko Mar 01 '18 at 00:53
  • Hi Erlangga, thanks for that reply. I had already put the methods in MyDbContext.cs but since there are too many SP methods, I don't want to crowd the MyDbContext.cs, so was asking for an alternative. – Nands Mar 02 '18 at 05:06
  • 1
    Hi Nands, don't forget that you have the option to use PARTIAL class to split the DbContext class into several files, so you can manage the methods better. – Erlangga Hasto Handoko Mar 02 '18 at 06:14
  • Thanks Erlangga, I will use your PARTIAL class suggestion! If you can put it as a separate answer, I would mark it as the answer. – Nands Mar 08 '18 at 05:21
  • I've just edited my answer above. I'm glad it can help you at the end. – Erlangga Hasto Handoko Mar 08 '18 at 10:14