Note that there are some explanatory texts on larger screens.

plurals
  1. POCode First and Parent-Child references
    primarykey
    data
    text
    <p>I'm using Code First CTP 5. I have a fairly simple setup between a parent table and child tables</p> <pre><code>Create table testA ( id int not null identity(1,1), stuff varchar(200), primary key (id) ); go create table testB ( id int not null foreign key references testA(id), morestuff varchar(200), primary key (id) ); go </code></pre> <p>To refer to these table using Code First, we have the following construct:</p> <pre><code>namespace Test.Models { public class TestEntities : DbContext { public DbSet&lt;testa&gt; testa { get; set; } public DbSet&lt;testb&gt; testb { get; set; } protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity&lt;testa&gt;().ToTable("testa"); modelBuilder.Entity&lt;testa&gt;() .Property(p =&gt; p.id) .HasDatabaseGenerationOption(DatabaseGenerationOption.Identity); modelBuilder.Entity&lt;testb&gt;().ToTable("testb"); } } public class testa { public int id { get; set; } public String stuff { get; set; } public virtual testb testb { get; set; } } public class testb { public int id { get; set; } public string morestuff { get; set; } public virtual testa testa { get; set; } } } </code></pre> <p>When I try to add a record to testa, I get the error "Cannot insert explicit value for identity column in table 'testA' when IDENTITY_INSERT is set to OFF." </p> <p>Ok. Strike 1 to Code First for not recognizing that Id is an identity column. We can fix this, so we tell CodeFirst that testa.id is an identity:</p> <pre><code> modelBuilder.Entity&lt;testa&gt;() .Property(p =&gt; p.id) .HasDatabaseGenerationOption(DatabaseGenerationOption.Identity); </code></pre> <p>That done we run it again and get another error: "A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'id'". So - what's wrong with this picture?</p> <p>What am I doing wrong and how do I fix it???</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload