title: Entity Framework automatically inserts new row in parent table
body: <p>I'm using an UI to insert data into the DB. Here is my entity model and controller. Whenever I'm inserting data into the <code>UserDetail</code> table, it automatically creates a new row in the <code>UserRole</code> table. Not sure why it's happening like this. For now <code>UserRole</code> is hard-coded in controller.</p>
  
  <pre><code>public class UserDetail
  {
          [Key]
          public int UserID { get; set; }
          [Required]
          [StringLength(30, MinimumLength = 4)]
          public string UserName { get; set; }
          [Required]
          [StringLength(50, MinimumLength = 4)]
          public string FirstName { get; set; }
          [Required]
          [StringLength(50, MinimumLength = 4)]
          public string LastName { get; set; }
          [Required]
          [EmailAddress]
          [StringLength(150, MinimumLength = 4)]
          public string Email { get; set; }
          [Required]
          [DataType(DataType.Password)]
          [StringLength(30,MinimumLength=4)]
          public string Password { get; set; }
          public UserRole UserRole { get; set; }
  }
  
  public class UserRole
  {
          [Key]
          public int RoleID { get; set; }
          [Required]
          [StringLength(20,MinimumLength=5)]
          public string RoleName { get; set; }
          public IEnumerable&lt;UserDetail&gt; UserDetail { get; set; }
  }
  
  [HttpPost]
  public HttpResponseMessage Register(UserDetail usrInfo)
  {
      UserContext ctx = new UserContext();
  
      UserDetail user = new UserDetail
              {
                  UserRole = ctx.UserRole.Where(id =&gt; id.RoleID == 2).Select(r =&gt; r).FirstOrDefault(),
                  FirstName = usrInfo.FirstName,
                  LastName = usrInfo.LastName,
                  UserName = usrInfo.UserName,
                  Password = usrInfo.Password,
                  Email = usrInfo.Email,
              };
      _unitofwork.userDetail.Add(user);
  
      if (_unitofwork.Completed() &gt; 0)
          return Request.CreateResponse(HttpStatusCode.OK, "Created");
      else
          return Request.CreateResponse();
  }
  public class UserContext: DbContext
      {
          public UserContext():base()
          {
              Database.SetInitializer&lt;UserContext&gt;(new CreateDatabaseIfNotExists&lt;UserContext&gt;());
              //Database.SetInitializer&lt;UserContext&gt;(new DropCreateDatabaseIfModelChanges&lt;UserContext&gt;());
          }
          public DbSet&lt;UserDetail&gt; UserDetails { get; set; }
          public DbSet&lt;UserRole&gt; UserRole { get; set; }
  
      }
  </code></pre>