I needed a 1:1
mapping of DivisionParticipant
and Team
with both IDs being auto increment. I did this because we wanted to use to Nullable FK's, Team
and Player
. Before Team ID
was the PK to DivisionParticipant ID
, but now DivisionParticipant ID
is auto increment and Team
is now added as a property with the FK to get the 1:1 working. This works fine and minimal code changes were needed when we do a read, but now on a save we get this error below. The ID of DivisionParticipant
was made to autoincrement so I am unsure what column it is speaking of?
Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF
Classes
public class DivisionParticipant
{
public int Id {get; set;}
public virtual Team Team { get; set; }
public virtual Player Player { get; set; }
}
public class Team
{
public int Id {get; set;}
public virtual DivisionParticipant DivisionParticipant { get; set; }
}
DataContext
modelBuilder.Entity<Team>()
.HasOptional(t => t.DivisionParticipant)
.WithRequired(t => t.Team);