Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've looked through existing questions surrounding this exception on SO but none seem to apply to my circumstances.

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

I am getting this exception when executing Update-Database in Package Manager Console.

My seed method 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();
}

My DbContext subclass contains the following fluent API definitions (trimmed for relevance):

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);

And finally the POCOs in question:

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 relationship between Schema and SchemaDictionary is meant to be of type one to zero-or-one...

  • Schema object will have zero or one SchemaDictionary
  • SchemaDictionary will always have a single Schema.

My understanding of the problem is that it cannot uphold the FK constraint because EF isn't aware of the GUID foreign key. How do I insert seed data when there is such a dependent relationship defined?

like image 935
Chris Pickford Avatar asked Feb 14 '23 18:02

Chris Pickford


1 Answers

When you do this:

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

You are Configuring a Required-to-Optional Relationship (One-to–Zero-or-One). You are specifying that the SchemaDictionary is the dependent and the Schema is the principal

When you have a one-to-one relationship in Entity Framework, the PK in the dependent must also be a FK to the principal. But you have specified that it should be database generated. The column cannot be both a FK and an IDENTITY so you get the error message. You should remove the HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) from the SchemaDictionaryId property

As an aside some of your code is unnecessary

  1. HasKey because EF will assume SchemaId and SchemaDictionaryID are keys by naming convention

  2. IsRequired() on fields that are not nullable

References:

When mapping, which is end is the principal?

What does principal end mean?

like image 145
Colin Avatar answered Feb 22 '23 22:02

Colin