Home > database >  Ef include linked table not getting updated
Ef include linked table not getting updated

Time:11-02

I have this ef method where I am using this Include and select the parent table using the Single Id for MeetingPollingId. I am sending a model to be created into the database. Its working fine for new record where I send a 0 for the MeetingPollingQuestionId but when I send a value where there is already a record I wanted it to update the data and not create a new record. Is there something else I need to change for the update to working?

public ResultStatus SaveMeetingPollingQuestion(Model.MeetingPollingQuestion mpq)
        {
            using (var db = new NccnEcommerceEntities())
            {
                using (DbContextTransaction dbTran = db.Database.BeginTransaction())
                {

                    ResultStatus result = new ResultStatus();

                    try
                    {


                        var meetingPolling = db.MeetingPollings
                            .Include(x => x.MeetingPollingQuestions)
                            .Include(x => x.MeetingPollingQuestions.Select(q => q.MeetingPollingParts))
                                .Single(x => x.MeetingPollingId == mpq.MeetingPollingId);

                        var newMeetingPollingQuestion = new EFModel.MeetingPollingQuestion
                        {
                            MeetingPollingQuestionId = mpq.MeetingPollingQuestionId,
                            MeetingPollingId = mpq.MeetingPollingId,
                            MeetingPollingQuestionTypeId = mpq.MeetingPollingQuestionTypeId,
                            SequenceOrder = mpq.SequenceOrder,
                            MeetingPollingParts = mpq.MeetingPollingParts.Select(p => new EFModel.MeetingPollingPart
                            {
                                MeetingPollingPartsTypeId = p.MeetingPollingPartsTypeId,
                                MeetingPollingPartsValues = p.MeetingPollingPartsValues.Select(v => new EFModel.MeetingPollingPartsValue
                                {
                                    QuestionValue = v.QuestionValue,
                                    MeetingPollingPartsValuesTypeId = v.MeetingPollingPartsValuesTypeId
                                }).ToList()
                            }).ToList()
                        };

                        meetingPolling.MeetingPollingQuestions.Add(newMeetingPollingQuestion);

                        db.SaveChanges();
                        dbTran.Commit();

                        result.ResultCode = Convert.ToInt32(Enums.Result.Code.Success);
                        result.Message = "Successfully uploaded.";
                    }
                    catch (Exception e)
                    {
                        dbTran.Rollback();
                        result.Message = "Error Save Meeting Polling Question"   e.Message;
                        result.ResultCode = Convert.ToInt32(Enums.Result.Code.Error);

                    }
                    return result;
                }
            }
        }

CodePudding user response:

What you are asking for is an "Upsert" which is not supported "out of the box" by Entity Framework. Upserts are supported in other query languages like PostgreSQL, etc.

You will have to "roll your own" Upsert functionality using Entity Framework. Since you are using .Add() in your example, you are only Insert-ing data, but never updating it. To do this, you will need to check if the question id already exists.

You can do something similar to this...

// determine whether the question id exists
var mpqIdExists = db.MeetingPollings.Where(mp => mp.MeetingPollingId == mpq.MeetingPollingId && mp.MeetingPollingQuestionId == mpq.MeetingPollingQuestionId).Any();

if (mpqIdExists) 
{
     // update
     db.Entry(newMeetingPollingQuestion).State = EntityState.Modified;
}
else 
{
     // insert
     meetingPolling.MeetingPollingQuestions.Add(newMeetingPollingQuestion);
}

db.SaveChanges();
dbTran.Commit();

Courtesy of: More efficient way to perform a UPSERT with EF6

  • Related