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 8351265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:34:51+00:00 2026-06-09T08:34:51+00:00

I have an issue where inserting one record to the entity ProfessionalEmploymentRecord EF ends

  • 0

I have an issue where inserting one record to the entity ProfessionalEmploymentRecord EF ends up duplicating existing Professional, Program, Role records in each entitiy, respectively. I have tried to explicitely initilize Professional, Program, Role with existing key/value pairs in my database, but EF still creates duplicate values with new keys.

public class ProfessionalEmploymentRecord
{
    public int ProfessionalEmploymentRecordId {get; set; }

    public virtual Professional Professional { get; set; }
    public virtual Program Program { get; set; }
    public virtual Role Role { get; set; }
}
public class Program
{
    public int ProgramId { get; set; }
    [MaxLength(20)]
    public string Name { get; set; }
}
public class Role
{
    public int RoleId { get; set; }
    [MaxLength(50)]
    public string Name { get; set; }
}
public class Professional
{
    public int ProfessionalId { get; set; }
    [MaxLength(20)]
    public string Name { get; set; }
}

When using Create below I end up with new records in the three related entities although they exist and have a key set. In other words I end up with copies of values with new primary keys (incremented by one) in the three related entities.

    public ActionResult Create(ProfessionalEmploymentRecord professionalemploymentrecord)
    {
        if (ModelState.IsValid)
        {
            db.ProfessionalEmploymentRecords.Add(professionalemploymentrecord);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }
        return View(professionalemploymentrecord);
    }

As requested

@model My.Models.ProfessionalEmploymentRecord
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend> Professional Employment Record</legend>

    <div class="editor-label">
        Professional
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Professional.Name)
        @Html.ValidationMessageFor(model => model.Professional.Name)
    </div>
    <div class="editor-label">
         Program
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Program.Name)
        @Html.ValidationMessageFor(model => model.Program.Name)
    </div>
    <div class="editor-label">
         Role
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Role.Name)
        @Html.ValidationMessageFor(model => model.Role.Name)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

I have used code below to test. (I see that executing SaveChanges sets *Id++ in reco).

    public void TestDummyAdd()
    {
        Professional prof = new Professional { ProfessionalId = 1, Name = "Chris" };
        Program prog = new Program { ProgramId = 1, Name = "A" };
        Role role = new Role { RoleId = 1, Name = "B" };
        ProfessionalEmploymentRecord reco = new ProfessionalEmploymentRecord { Professional = prof, Role = role, Program = prog, ProfessionalEmploymentRecordId = 0 };

        if (ModelState.IsValid)
        {
            db.ProfessionalEmploymentRecords.Add(reco);
            db.SaveChanges();
        }
    }
  • 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-06-09T08:34:54+00:00Added an answer on June 9, 2026 at 8:34 am

    Add is operation executed on whole object graph. If you add your record you are also adding all its relations, their relations, etc. There are two options how to solve this issue and both require you to manually set state of some entities (and sometimes also relations) in your persisted object grap:

    • Call Add as you did but after that you must set state for all existing entities in the graph to either Unchanged or Modified (entity will be udpated in database)
    • Call Attach instead of Add and after that set state for all new entities to Added

    Settings state can be done by:

    db.Entry(entity).State = EntityState.Added;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've ran into a puzzling issue: I have only one method defined for inserting
I have an interesting issue with inserting rows of data into a dataview. I
I have an issue about Entity Framework 4 CTP 5, which I realize LINQ
I'm new to CodeIgniter and have come into an issue with the insertion of
I have issue with: <form:checkboxes path=roles cssClass=checkbox items=${roleSelections} /> If previous line is used
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
We are new to ROR, We have issue in creating Login/Logout process in ROR
Share your ideas please! I have issue to check the folder and convert a
Have an issue with marshall and unmarshall readers and writers. So here it is.
I have an issue with jquery and history.back(): I got a link: <a href=#

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.