EF migrations exception: A dependent property in a ReferentialConstraint is mapped to a store-generated column

asp.net-mvc-4 c# ef-migrations entity-framework

Question

I've searched through the questions already posted on SO about this exception, but none seem to be relevant to my situation.

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'SchemaDictionaryId'.

When I execute, I receive this exception.Update-Database from the Package Manager Console.

What I do is as follows:

protected override void Seed(DataEntry.App_Data.DataEntryDataContext context)
{
    context.Schemas.AddOrUpdate(s => s.SchemaId, _generateSchemaData());
    context.SaveChanges();
}

private Schema[] _generateSchemaData()
{
    List<Schema> schemas = new List<Schema>();
    // Loop removed for simplicity
    schemas.Add(new Schema
    {
        // various property assignments
        SchemaDictionary = new SchemaDictionary
        {
            // various property assignments
        }
    });
    return schemas.ToArray();
}

The following fluent API declarations are included in my DbContext subclass (reduced for clarity):

modelBuilder.Entity<Schema>()
    .HasKey(s => s.SchemaId);

modelBuilder.Entity<Schema>()
    .Property(s => s.SchemaId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    .IsRequired();

modelBuilder.Entity<SchemaDictionary>()
    .HasKey(sd => sd.SchemaDictionaryId);

modelBuilder.Entity<SchemaDictionary>()
    .Property(s => s.SchemaDictionaryId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    .IsRequired();

modelBuilder.Entity<SchemaDictionary>()
    .HasRequired(sd => sd.Schema)
    .WithOptional(s => s.SchemaDictionary);

The POCOs in question are as follows:

public class Schema
{
    public Guid SchemaId { get; set; }

    public Nullable<Guid> SchemaDictionaryId { get; set; }
    public virtual SchemaDictionary SchemaDictionary { get; set; }

    // various other properties
}

public class SchemaDictionary
{
    public Guid SchemaDictionaryId { get; set; }

    public Nullable<Guid> SchemaId { get; set; }
    public virtual Schema Schema { get; set; }

   // various other properties
}

The connection betweenSchema and SchemaDictionary is intended to be a zzz-42 zzz kind...

  • Schema object will have a value of 0 or 1.SchemaDictionary
  • SchemaDictionary will only ever have oneSchema .

My interpretation of the issue is that EF is unaware of the FK constraint, therefore it cannot uphold it.GUID exotic key. When there is such a stated dependant relationship, how can I insert seed data?

1
4
11/5/2013 4:13:56 PM

Accepted Answer

After doing this:

modelBuilder.Entity<SchemaDictionary>()
    .HasRequired(sd => sd.Schema)
    .WithOptional(s => s.SchemaDictionary);

You are 11 zzz zzz. Your declaration that theSchemaDictionary the reliant and theSchema is the teacher

In Entity Framework, the PK in the dependant must also be an FK to the principal when there is a one-to-one relationship. However, you have stated that it must be generated from a database. There is a mistake since the column cannot be both an FK and an IDENTITY. You should get rid ofHasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) using theSchemaDictionaryId property

Aside: Some of your code is superfluous.

  1. HasKey since EF will presumeSchemaId and SchemaDictionaryID by naming convention, are keys

  2. IsRequired() on fields that can't be null

References:

Which end is the principal when mapping?

What is the meaning of principal end?

8
5/23/2017 12:29:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow