0

In my company, we are using Telerik OpenAccess. An extra event is added, every time a dbContext.SaveChanges() is called.

A beginning programmer has used this dbContext.SaveChanges() no less than 93 (!!!) times, causing numerous problems with my application (the corresponding event should be launched one or two times, not 93 times).

I have already tried removing the dbContext.SaveChanges() but as the modifications in the database are really necessary, that creates a large failure in the program.

Hence, the possibly weird question: is there another piece of code than dbContext.SaveChanges() which saves the changes in database, referred to by dbContext?

If such a thing does not exist, I might also agree to a solution like:

var events = dbContext.getEvents();
dbContext.RemoveEvents();
dbContext.SaveChanges();
dbContext.AddEvents(events);

For your information, a dbContext is defined as:

private readonly IUnityContainer _unityContainer;
using (var dbContext = _unityContainer.Resolve<IDatabaseConnection>())
{
    ... // all DB-related actions happen inside such a using-clause.
}

The UnityContainer is defined in Microsoft.Practices.Unity and has the following heading:

//
// Summary:
//     Interface defining the behaviour of
//     the Unity dependency injection container.
public interface IUnityContainer : IDisposable

If you can say that there's a way to get another dbContext (not firing the mentioned events), that would be great, something like:

using (var anotherDbContext =  
       _unityContainer.Resolve_another<IDatabaseConnection>())
{
    ... // I can also make DB modifications, but without those events.
}

Solution proposal: what about CreateDetachedCopy?

I've just found the method Telerik OpenAccess CreateDetachedCopy(), and I imagine it works as follows:

var obj = dbContext.CreateDetachedCopy<Class1>(o => o.id==known_id);
obj.ChangeSomething();
dbContext.SaveChanges(); // the modifications in obj are 
                         // not taking into account.

Now, the question becomes, how can I make sure that, on a more suited moment, I can make sure the modifications do get taken into account?

var obj2 = dbContext.GetData<Class1>(o => o.id == obj.id).FirstOrDefault();
obj2.CloneInformationFromObject(obj); 
          // just a clear name, I have no idea how to do this.
dbContext.SaveChanges();

How can I perform the CloneInformationFromObject() part? Is this covered by Telerik OpenAccess library?

Beware:
My dbContext is just an interface to the Telerik OpenAccess dbContext, and this interface does not contain the method AttachCopy(), so I can't use that.

2
  • Please provide more information. What is dbContext? this is EntityFramework context or some telerik object? What particular event called? etc. Commented May 23, 2024 at 16:09
  • @OlehIvanitskyy: I've added the information, based on Unity dependency injection. Commented May 24, 2024 at 6:15

0

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.