0
votes

I'm using EF Edmx, with lazy loading off. I get an item with that has a virtual list as a navigation property to other object.

ex:

public partial class CalculationInstance{
     public virtual ICollection<ServiceOffer> ServiceOffers { get; set; }
}

I have a method to get a CalculationInstance, where I do not include the ServiceOffers.

Later in the code I want to make an attribution to the ServiceOffers of that object instance:

var cInst = GetCalculationInstance(id);
cInst.ServiceOffers = GetListOfServiceOffers();

Later, when I try to use ServiceOffers, it throws exception of Context object disposed. As it's trying to get them from database, instead of just using the service offers that I just attributed.

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

My goal is to fill the object with my own list and not from database, is there a way to do this?

EDIT: for reference:

public CalculationInstance GetCalculationInstance(Guid id) {
        PortugalInternalCalculatorDb ctx = null;
        try {
            ctx = _ciContextFactory.Create();
            return ctx.CalculationInstances
                .Include(l => l.CompatibilityGroup)
                .Include(l => l.CalculationAnswers)
                .SingleOrDefault(l => l.Id == id);
        } finally {
            if (ctx != null)
                _ciContextFactory.Release(ctx);
        }
    }

public List<ServiceOffer> GetServiceOffers(Guid calcInstanceId, string partner = null) {
            PortugalInternalCalculatorDb ctx = null;
            try {
                ctx = _ciContextFactory.Create();
                return
                    ctx.ServiceOffers
                        .Include(_ => _.ServiceOfferFeatures)
                        .Include(_ => _.Provider)
                        .Where(_ => _.CalculationInstanceId == calcInstanceId
                        && (partner == null || _.Provider.CommunicatorPrefix.ToLower().Equals(partner)))
                        .ToList();
            } finally {
                if (ctx != null)
                    _ciContextFactory.Release(ctx);
            }
        }
What type does the GetListOfServiceOffers() return? It might be solved with: cInst.ServiceOffers = GetListOfServiceOffers().ToList(); - Jeroen van Langen
it already returns a list. List<ServiceOffer> - Batista
Then you should create a class to store the informatie a ServiceOffer contains and 'copy' the information. - Jeroen van Langen
What do you mean? have a second list in CalculationInstance? :/ - Batista
Please, provide GetCalculationInstance(id) and GetListOfServiceOffers() - Ingweland