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 oneSchemaDictionary
SchemaDictionary
will always have a singleSchema
.
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?