Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Closed Issue: EF Stored Procs :: Many-Many self reference mapped to stored procedures throws [1245]

$
0
0
Repro:

Create the following model:

```
public class ArubaPerson
{
public int Id { get; set; }
public string Name { get; set; }
public ArubaPerson Partner { get; set; }
public ICollection<ArubaPerson> Children { get; set; }
public ICollection<ArubaPerson> Parents { get; set; }
}

public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ArubaPerson>().HasOptional(p => p.Partner).WithOptionalPrincipal();
modelBuilder.Entity<ArubaPerson>().HasMany(p => p.Children).WithMany(p => p.Parents).MapToStoredProcedures();
}
}

```


Enable-Migrations
Add-Migrations Mig1

this throws the following exception:

__An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
__ at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation)
at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassa.<Attach>b__9()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
at System.Data.Entity.DbSet.Attach(Object entity)
at System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator.<GenerateAssociation>d__0`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](StorageAssociationSetModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String rowsAffectedParameterName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateInsertFunctionBody(StorageAssociationSetModificationFunctionMapping modificationFunctionMapping, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<BuildCreateProcedureOperations>d__d0.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, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)


There used to be a similar bug with mapping entities with self reference and auto generated key. Perhaps we should throw similar exception. However, even if one adds the following line, the issue persists.


```
modelBuilder.Entity<ArubaPerson>()
.Property(e => e.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

```
Comments: Verified, closing

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>