• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1102
  • Last Modified:

WCF Service with Entity Framework.

Hello

I am trying to create a WCF Service Library that uses Entity Framework to insert or update a record in the table.

When I try to insert a record I keep on getting - "Error retrieving values from ObjectStateEntry. See inner exception for details." at the save changes of the insert method.

Code -

 public bool UpdateResultRecord(ref Results result)
        {

            IRWSEORankingDBEntities context = new IRWSEORankingDBEntities();

            int resultID = result.result_id;

            if (resultID == 0)
            {
                InsertResultsRecord(ref result);
            }
            else
            {
                ResultEntity resultInDB = (from r
                                           in context.ResultEntities
                                           where r.result_id == resultID
                                           select r).FirstOrDefault();

                if (resultInDB == null)
                {
                    InsertResultsRecord(ref result);
                }
                else
                {
                    context.Detach(resultInDB);

                    // update the result
                    resultInDB.customer_id = result.customer_id;
                    resultInDB.campaign_id = result.campaign_id;
                    resultInDB.adword_id = result.adword_id;
                    resultInDB.ranking = result.ranking;
                    resultInDB.found = result.found;
                    resultInDB.result_date = result.result_date;
                    // attach it
                    context.Attach(resultInDB);
                    // change object state
                    context.ObjectStateManager.ChangeObjectState(resultInDB, System.Data.EntityState.Modified);
                    context.SaveChanges();
                }
            }

            context.Dispose();


            return true;
        }

        private void InsertResultsRecord(ref Results result)
        {
            //Insert Record
            IRWSEORankingDBEntities theContext = new IRWSEORankingDBEntities();

            ResultEntity newRecord = new ResultEntity();

            newRecord.customer_id = result.customer_id;
            newRecord.campaign_id = result.campaign_id;
            newRecord.adword_id = result.adword_id;
            newRecord.ranking = result.ranking;
            newRecord.found = result.found;
            newRecord.result_date = result.result_date;
           
            theContext.ResultEntities.AddObject(newRecord);
            theContext.SaveChanges();

            theContext.Dispose();
        }


    }

Does anybody have any ideas, this is the first time I've used WCF and Entity Framework.

Regards

Kev.

0
KABarrie
Asked:
KABarrie
  • 2
1 Solution
 
grayeCommented:
So, what did the Inner Exception have to say about the reason for the error?
0
 
KABarrieAuthor Commented:

It was something to do with a column in the table was set to not null, yet the model was saying that it was nullable.

I deleted the model and re-created it this morning.  Seems to have solved the problem.

Thanks

Kevin.
0
 
KABarrieAuthor Commented:
It worked.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now