0

I'm initializing dbContext in my controller as below:

public class BaseController : Controller
{
    protected KContext db;

    public BaseController()
    {
        db = new KContext();
    }
}

and I do the usual add,edit, delete stuff in my controllers, I also have a class called serviceChecker which inherits registry (from FluentScheduler), and I have another dbcontext initialized there as below:

public class ServiceChecker : Registry
{
    KContext db;
    public ServiceChecker()
    {
        db = new KContext();
    }
}

and I check some tables in this class and edit some entities which are also get updated from my controllers. Now the problem I'm facing is that after I change an entity, let's call it serviceDoman's field endDate from 7/11/2014 to 7/11/2016, it updates the entity fine but when the servicechecker triggers the check function( every 60 sec), it has to update another field of that entity which happens fine but it also changes the endDate back to 7/11/2014(old value before the first edit), what possibly could be the cause?

Sid M
  • 4,354
  • 4
  • 30
  • 50
arash moeen
  • 4,533
  • 9
  • 40
  • 85

1 Answers1

0

Possible reason for getting old values after updating your entity is you are not calling SaveChanges() method to save entity changes and you are creating a new context object in ServiceChecker class which doesn't have any idea about the changes you made in an entity with your previous(another) context object.

One solution could be you call SaveChanges() method after you make changes in an entity.

Note: Its a good practice to use a common context object for performing all operations

Sid M
  • 4,354
  • 4
  • 30
  • 50
  • Nah, I'm usign repository pattern here and I'm calling my save function which calls SaveChanges(), I also check the updated value done by controller and it's updated but exactly when the serviceChecker calls the check function it uses the old data, somehow like the dbcontext in my serviceChecker has not been updated or something like that... kinda lost here – arash moeen Jun 25 '14 at 08:35
  • could you please give me a tip about using a common context for both my BaseController and my ServiceChecker? thanks in advance – arash moeen Jun 25 '14 at 08:36
  • Create a `Base` or `AbstractRepository` in which you create your context or `DbContext` object and inherit other repository from that Base repository, by doing this you will have created only one context object in your application. I'd suggest you to use a `DbContext` object. – Sid M Jun 25 '14 at 08:47
  • I already have a base repository which takes the initialized dbcontext .basically I initialize the dbcontext in my controller or my ServiceChecker and then pass it to each repository and they are all inherit from my base repository. I know this concept is not the best, it was implemented by another guy and I'm forced to just fix the problmes – arash moeen Jun 25 '14 at 08:53
  • Just use dependency injection and let some container manage the lifecycle of your `DbContext`. – ta.speot.is Jun 25 '14 at 08:53