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

Edited Issue: CF Stored Procs :: Misleading exception thrown when trying to map entity with composite key, being a dependent of 1-Many relationship [1242]

$
0
0
Repro.
Produce the following model:

```
public class ArubaTask
{
public int Id { get; set; }
public string Name { get; set; }
}

public class ArubaRun
{
public int Id { get; set; }
public ICollection<ArubaTask> Tasks { get; set; }
}

public class ArubaContext : DbContext
{
public DbSet<ArubaTask> Tasks { get; set; }
public DbSet<ArubaRun> Runs { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ArubaTask>().HasKey(k => new { k.Id, k.Name });
modelBuilder.Entity<ArubaRun>().HasMany(r => r.Tasks).WithRequired().Map(m => { });
modelBuilder.Entity<ArubaRun>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<ArubaTask>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}

```
Enable-Migrations
Add-Migration Mig1

now add the following line to the OnModelCreating (map all types to stored procs):

modelBuilder.Types().Configure(t => t.MapToStoredProcedures());

The following exception is thrown (note that affected type has neither store generated key nor self reference.


__An error occurred while attempting to generate the body SQL of the stored procedure 'ArubaTask_Update' for entity type 'ArubaTask'. 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 'ArubaRun_Tasks' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'ArubaRun_Tasks_Target' 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.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction, Boolean throwOnClosedConnection)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(Boolean throwOnClosedConnection)
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__33()
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.Core.Objects.ObjectContext.<>c__DisplayClass28.<SaveChanges>b__25()
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, Lazy`1 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, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String functionName, String rowsAffectedParameterName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateUpdateFunctionBody(StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Lazy`1 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, 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)





Viewing all articles
Browse latest Browse all 10318

Trending Articles



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