1

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.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
barnacle.m
  • 2,070
  • 3
  • 38
  • 82
  • Have you tried setting making `CumulativeRecordId` a primary key to the table `CumulativeRecord` ? EF can't do updates on tables without a primary key. – Hugo Nava Kopp May 11 '16 at 12:15
  • CumulativeRecordId is the primary key by default, if you follow convention. However I will try adding a `[Key]` anyway – barnacle.m May 11 '16 at 12:17

1 Answers1

0

The error answers the question. You are trying to insert a specific value into the CumulativeRecordId field, but in the database this is the primary key of the CumulativeRecord table.

It's set as IDENTIY which means that it will auto-increment, so you should never need to insert a value in this column.

Also, as it's a 1 - 1 relationship you need foreign keys on both sides of the relationship as here.

Your classes should look as follows

public class CumulativeRecord
{
    public int CumulativeRecordId { get; set; }

    public int AnaRecordId { get; set; }
    [ForeignKey("AnaRecordId")]
    public virtual ANA ANA { get; set; }
}

public class ANA
{
    public int ANAId { get; set; }

    public int CumulativeRecordId { get; set; }
    [ForeignKey("CumulativeRecordId")]
    public virtual CumulativeRecord CumulativeRecord { get; set; }
}
Community
  • 1
  • 1
Barry O'Kane
  • 1,189
  • 7
  • 12
  • See my updated attempt at saving the object, i'm not trying to insert a key manually, at least I don't think I am. – barnacle.m May 11 '16 at 12:35
  • Modified my answer slightly. Have you defined your foreign key as above? – Barry O'Kane May 11 '16 at 12:44
  • getting a new error by doing things this way, `ANA_CumulativeRecord_Target: : Multiplicity is not valid in Role 'ANA_CumulativeRecord_Target' in relationship 'ANA_CumulativeRecord'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'` – barnacle.m May 11 '16 at 12:45
  • @barnacle.m Have a look at my changes now. You need foreign keys on both sides of the relationship if it's 1 - 1 apparently. See here http://stackoverflow.com/questions/18724964/entity-framework-1-to-1-relationship-using-code-first-how. I have this working locally. – Barry O'Kane May 11 '16 at 13:12
  • Thanks, is there anything I should modify in my context class using Fluent? – barnacle.m May 11 '16 at 13:16
  • I didn't change anything if that's what you're asking, but otherwise I don't see a problem. – Barry O'Kane May 11 '16 at 13:19
  • `ANA` should *not* have a foreign key to `CumulativeRecordId` and `CumulativeRecord` no `AnaRecordId`. `CumulativeRecordId` as both primary key and foreign key is enough for required 1:1 associations. – Gert Arnold May 11 '16 at 14:10
  • @GertArnold as per the link I included that was one of the options, and if a foreign key exists in one it needs to exist in the other. I did try removing the foreign keys and removing virtual from the navigation properties and this worked for me also. – Barry O'Kane May 11 '16 at 15:28
  • @barnacle.m if this answer works for you, please remember to accept it as the answer to the question. – Barry O'Kane May 17 '16 at 11:22