Weird error: The following objects have not been refreshed

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
na95mwn
Posts: 3
Joined: Sat 12 Feb 2011 13:30
Location: SE

Weird error: The following objects have not been refreshed

Post by na95mwn » Sat 12 Feb 2011 13:46

Hello,

We have a model with an entity called "Connection". The key is taken from an oracle sequence we have specified in the model.
When we try to create a new connection we sometimes get this error.
Error in GetPortfolioIdByAO:

System.InvalidOperationException: The following objects have not been refreshed because they were not found in the store:

'EntitySet=ConnectionSet;CONNECTION_ID=186'.

at System.Data.Objects.ObjectContext.RefreshEntities(RefreshMode refreshMode, IEnumerable collection)

at System.Data.Objects.ObjectContext.Refresh(RefreshMode refreshMode, Object entity)

at Forsmark.BusinessProcesses.UH.BusinessComponents.Portfolio.PortfolioLogic.ConnectAO(Int32 portfolioId, Int32 aroNr)

at Forsmark.BusinessProcesses.UH.BusinessComponents.Portfolio.PortfolioLogic.GetPortfolioIdByAO(Int32 aroNr)

at Forsmark.BusinessProcesses.UH.BusinessComponents.Portfolio.BC_Portfolio.GetPortfolioIdByAO(Int32 aroNr)
The code is like this. The error occurs when "if (connection == null)" is true and we call Create and ConnectAO directly after eachother.

Code: Select all

        public int GetPortfolioIdByAO(int aroNr)
        {
            using (var context = new PortfolioEntities(this.connectionString))
            {
                var connection = context.ConnectionSet.FirstOrDefault(x => x.ARO_NR == aroNr);

                int portfolioId;

                if (connection == null)
                {
                    portfolioId = this.Create();
                    this.ConnectAO(portfolioId, aroNr);
                }
                else
                {
                    portfolioId = connection.CONNECTION_ID;
                }

                return portfolioId;
            }
        }

        public void ConnectAO(int portfolioId, int aroNr)
        {
            using (var context = new PortfolioEntities(this.connectionString))
            {
                var connection = context.ConnectionSet
                    .First(x => x.CONNECTION_ID == portfolioId);

                if ((connection.ARO_NR ?? aroNr) != aroNr)
                {
                    throw new Exception("Portfolio redan kopplad till annan arbetsorder!");
                }

                connection.ARO_NR = aroNr;

                try
                {
                    context.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    context.Refresh(RefreshMode.ClientWins, connection);
                    context.SaveChanges();
                }

            }
        }

        public int Create()
        {
            using (var context = new PortfolioEntities(this.connectionString))
            {
                var connection = new Connection();
                context.AddToConnectionSet(connection);

                context.SaveChanges();

                return connection.CONNECTION_ID;
            }
        }

[/code]

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Mon 14 Feb 2011 15:26

I have made several tests and succeeded. Are you using TransactionScope or any other components? If possible, please send us (support * devart * com, subject "EF: Refresh problem") a small test project illustrating the issue?

Post Reply