I am trying to create a one to one relationship between two entities using EF, however when I try to actually create the CumulativeRecord
entity I get this error on db.SaveChanges()
:
Cannot insert explicit value for identity column in table 'CumulativeRecords' when IDENTITY_INSERT is set to OFF.
My two models are as follows:
public class CumulativeRecord
{
[ForeignKey("ANA")]
public int CumulativeRecordId { get; set; }
public virtual ANA ANA { get; set; }
}
public class ANA
{
public int ANAId { get; set; }
public virtual CumulativeRecord CumulativeRecord { get; set; }
}
and using the Fluent API I have:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ANA>()
.HasRequired(a => a.CumulativeRecord)
.WithRequiredPrincipal(a => a.ANA);
}
Despite this setup, I am unable to save a new CumulativeRecord
object.
This is my current attempt at saving the object:
var experience = new CumulativeRecord { ANA = newANA };
db.CumulativeRecords.Add(experience);
db.SaveChanges();
It is also worth noting that I am not working with a new database, this database exists and I am adding models and relationships to it.