Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7553213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:59:12+00:00 2026-05-30T10:59:12+00:00

I need help with trying to insert a record using MVC and Entity Framework.

  • 0

I need help with trying to insert a record using MVC and Entity Framework. I have a dynamically created form which can contain many questions. When Editing, I want to delete the existing answers (which it does successfully) and insert new answers.

I am getting the following error:
Cannot insert explicit value for identity column in table ‘tblModeratorReportAnswers’ when IDENTITY_INSERT is set to OFF.

If I add the following line in my DbContext class
modelBuilder.Entity<QuestionAnswer>().Property(p => p.AnswerID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); I get this error:
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: ‘AnswerID’.

Here’s my code that is doing the update

//
// POST: /Home/Edit/1
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(FormCollection formCollection, int moderatorReportId)
{
    ModeratorReport reportToEdit = repository.GetModeratorReportById(moderatorReportId);
    List<QuestionAnswer> originalReportAnswers = repository.GetAllModeratorReportAnswers(moderatorReportId).ToList();

    foreach (QuestionAnswer answer in originalReportAnswers) {
        repository.DeleteAnswer(answer);
    }

    repository.Save();

    int sectionID;
    int questionID;

    foreach (string key in formCollection.AllKeys)
    {
        var value = formCollection[key.ToString()];

        Match m = Regex.Match(key, "section(\\d+)_question(\\d+)");

        if (m.Success) {
            QuestionAnswer newAnswer = new QuestionAnswer();

            sectionID = Convert.ToInt16(m.Groups[1].Value.ToString());
            questionID = Convert.ToInt16(m.Groups[2].Value.ToString());

            newAnswer.ModeratorReportID = moderatorReportId;
            newAnswer.QuestionID = questionID;
            newAnswer.Answer = value;
            repository.AddAnswer(newAnswer);
        }
    }

    repository.Save();

    reportToEdit.Status = "SUBJECTOFFICER SAVED";

    AuditItem auditItem = new AuditItem();
    auditItem.ModeratorReportID = moderatorReportId;
    auditItem.Status = "SUBJECTOFFICER SAVED";
    auditItem.AuditDate = DateTime.Now;
    auditItem.Description = "The Moderator report ID: " + moderatorReportId + " was saved.";
    auditItem.UserID = User.Identity.Name;

    db.Audit.Add(auditItem);

    repository.Save();

    return RedirectToAction("Details", new { id = moderatorReportId });
}

…and in my repository

//
// Persistance

public void Save()
{
    db.SaveChanges();
}

public void AddAnswer(QuestionAnswer answer)
{
    db.Answers.Add(answer);
    Save();
}

public void DeleteAnswer(QuestionAnswer answer) 
{
    db.Answers.Attach(answer);
    db.Answers.Remove(answer);
}

I have also checked all my Primary Keys, Foreign Keys and they are all ok. The Primary Keys are all set to ‘Is Identity’.

I’ve been trying to sort this problem out all day. I have no idea what to do to resolve it. If anyone can give my any advice, it’d be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:59:14+00:00Added an answer on May 30, 2026 at 10:59 am

    Maybe it’s my inexperience with ASP.NET MVC and Entity Framework, but I have now resolved this issue by changing the logic of that I update the report.

    Instead of deleting the answers and reinserting them. I now retrieve the answers and change Answer property to be the new answer. Then just use db.SaveChanges().

    //
    // POST: /Home/Edit/1
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(FormCollection formCollection, int moderatorReportId)
    {
        ModeratorReport reportToEdit = repository.GetModeratorReportById(moderatorReportId);
        List<QuestionAnswer> originalReportAnswers = repository.GetAllModeratorReportAnswers(moderatorReportId).ToList();
    
        int sectionID;
        int questionID;
    
        foreach (string key in formCollection.AllKeys)
        {
            var value = formCollection[key.ToString()];
    
            Match m = Regex.Match(key, "section(\\d+)_question(\\d+)");
    
            if (m.Success) {
                QuestionAnswer newAnswer = new QuestionAnswer();
    
                sectionID = Convert.ToInt16(m.Groups[1].Value.ToString());
                questionID = Convert.ToInt16(m.Groups[2].Value.ToString());
    
                foreach(QuestionAnswer answerToEdit in originalReportAnswers) {
                    if (answerToEdit.QuestionID == questionID)
                    {
                        answerToEdit.Answer = value;
                    }
                }
            }
        }
    
        repository.Save();
    
        reportToEdit.Status = "SAVED";
    
        AuditItem auditItem = new AuditItem();
        auditItem.ModeratorReportID = moderatorReportId;
        auditItem.Status = "SAVED";
        auditItem.AuditDate = DateTime.Now;
        auditItem.Description = "The Moderator report ID was saved.";
        auditItem.UserID = User.Identity.Name;
    
        db.Audit.Add(auditItem);
    
        repository.Save();
    
        return RedirectToAction("Details", new { id = moderatorReportId });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help, i trying to figure what i need to do, i have
I really need some help with this as I have been trying to fix
I have a CKEditor custom plugin I'm trying to write, but need some help
I'm trying to insert a new event into Google Calendar, which I can do
I need help with my football manager tournament. I am trying to insert all
I need some help trying to match a C include file with full path
Hey all i am in need of some help trying to figure out how
I need some help with jQuery script again :-) Just trying to play with
I need some help calculating Pi. I am trying to write a python program
I need some help with SQL Query. I am trying to select all records

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.