When initializing the following model:
public class CogTag
{
public Guid Id { get; set; }
public virtual Gear Gear { get; set; }
}
public class Gear
{
public int Id { get; set; }
public string Nickname { get; set; }
// 1 - 1 reference
public virtual CogTag Tag { get; set; }
// 1 - many self reference
public virtual ICollection<Gear> Reports { get; set; }
}
public class GearsOfWarContextSPBug : DbContext
{
public GearsOfWarContextSPBug()
{
Configuration.ValidateOnSaveEnabled = false;
}
static GearsOfWarContextSPBug()
{
Database.SetInitializer(new DropCreateDatabaseAlways<GearsOfWarContextSPBug>());
}
public DbSet<Gear> Gears { get; set; }
public DbSet<CogTag> Tags { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Gear>().HasRequired(g => g.Tag).WithOptional(t => t.Gear);
modelBuilder.Entity<Gear>().MapToStoredProcedures();
}
}
I get the following exception:
System.InvalidOperationException: An error occurred while attempting to generate the body SQL of the stored procedure 'Gear_Update' for entity type 'Gear'. This can happen if the entity type has both a self-referencing association and a store-generated key. See the inner exception for details. --->
System.Data.Entity.Core.UpdateException: A relationship from the 'Gear_Tag' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Gear_Tag_Source' must also in the 'Added' state.
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.RelationshipConstraintValidator.ValidateConstraints()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, Boolean throwOnExistingTransaction, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, Boolean throwOnExistingTransaction)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 func)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator.<Generate>d__12`1.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateFunctionBody[TCommandTree](StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String functionName, String rowsAffectedParameterName)
--- End of inner exception stack trace ---
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateFunctionBody[TCommandTree](StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String functionName, String rowsAffectedParameterName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateUpdate
FunctionBody(StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<BuildCreateProcedureOperations>d__cd.MoveNext()
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
at System.Data.Entity.Database.Create(Boolean skipExistsCheck)
at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
The exception goes away if key of the CogTag entity is changed to int
public class CogTag
{
public Guid Id { get; set; }
public virtual Gear Gear { get; set; }
}
public class Gear
{
public int Id { get; set; }
public string Nickname { get; set; }
// 1 - 1 reference
public virtual CogTag Tag { get; set; }
// 1 - many self reference
public virtual ICollection<Gear> Reports { get; set; }
}
public class GearsOfWarContextSPBug : DbContext
{
public GearsOfWarContextSPBug()
{
Configuration.ValidateOnSaveEnabled = false;
}
static GearsOfWarContextSPBug()
{
Database.SetInitializer(new DropCreateDatabaseAlways<GearsOfWarContextSPBug>());
}
public DbSet<Gear> Gears { get; set; }
public DbSet<CogTag> Tags { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Gear>().HasRequired(g => g.Tag).WithOptional(t => t.Gear);
modelBuilder.Entity<Gear>().MapToStoredProcedures();
}
}
I get the following exception:
System.InvalidOperationException: An error occurred while attempting to generate the body SQL of the stored procedure 'Gear_Update' for entity type 'Gear'. This can happen if the entity type has both a self-referencing association and a store-generated key. See the inner exception for details. --->
System.Data.Entity.Core.UpdateException: A relationship from the 'Gear_Tag' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Gear_Tag_Source' must also in the 'Added' state.
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.RelationshipConstraintValidator.ValidateConstraints()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, Boolean throwOnExistingTransaction, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, Boolean throwOnExistingTransaction)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 func)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator.<Generate>d__12`1.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateFunctionBody[TCommandTree](StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String functionName, String rowsAffectedParameterName)
--- End of inner exception stack trace ---
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateFunctionBody[TCommandTree](StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String functionName, String rowsAffectedParameterName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateUpdate
FunctionBody(StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<BuildCreateProcedureOperations>d__cd.MoveNext()
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, ModificationCommandTreeGenerator modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
at System.Data.Entity.Database.Create(Boolean skipExistsCheck)
at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
The exception goes away if key of the CogTag entity is changed to int