Here's the relevant parts of my code:
public partial class Users
{
public string Name { get; set; }
public Guid RoleId { get; set; }
public virtual Role UserRole {get; set; }
}
public partial class Role
{
public string RoleName {get; set; }
public Guid RoleId {get; set; }
}
RoleId is a primary key in the Roles table which has RoleName and RoleId. Users has Name and RoleId.
After setting up the User, I attempt to add them
this.Entities.Users.Add(user);
this.Entities.SaveChanges();
However, I get a DbUpdateException of type Violation of PRIMARY KEY in the Roles table. From what I am seeing, it is trying to insert the Role attached to the user, which obviously would cause that problem.
I tried putting a [NotMapped] attribute on the UserRole, however that did not fix the issue.
EF generated both of those classes.
Do I need to do some kind of schema change to make it not include UserRole and refactor to use the RoleId in users and reference everything that way, or is there something else that I am missing?
EDIT: Did a shallow copy grabbing the required information and it works now.