Skip to content

Support cascade updates for owned types/aggregates #10551

Open
@nickverschueren

Description

When I attach an entity that has an owned type attached to it and I change the state of that entity using the ChangeTracker to EntityState.Added, the owned type should also get the EntityState.Added state, but it doesn't.

Exception message: The entity of 'Entity' is sharing the table 'Entity' with 'Entity.OwnedType#OwnedType', but there is no entity of this type with the same key value that has been marked as 'Added'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.'
Stack trace:    at Microsoft.EntityFrameworkCore.Update.Internal.ModificationCommandIdentityMap.Validate(Boolean sensitiveLoggingEnabled)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.CreateModificationCommands(IReadOnlyList`1 entries, Func`1 generateParameterName)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.<BatchCommands>d__8.MoveNext()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at EfCoreState.Program.Main(String[] args) in c:\users\nickv\source\repos\projects\EfCoreState\EfCoreState\Program.cs:line 19

Steps to reproduce

    class Program
    {
        static void Main(string[] args)
        {
            var entity = new Entity();
            entity.OwnedType.SomeText = "Hello world!";

            var context = new TestContext();
            context.Database.EnsureCreated();

            context.Attach(entity);
            context.ChangeTracker.Entries<Entity>().First().State = EntityState.Added;
            context.SaveChanges();

            context.Database.EnsureDeleted();
        }
    }


    public class Entity
    {
        public Entity()
        {
            Id = Guid.NewGuid();
            OwnedType = new OwnedType();
        }

        public Guid Id { get; set; }

        public OwnedType OwnedType { get; set; }
    }

    public class OwnedType
    {
        public string SomeText { get; set; }
    }

    public class TestContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true");

            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var entity = modelBuilder.Entity<Entity>();
            entity.HasKey(e => e.Id);
            entity.OwnsOne(e => e.OwnedType);

            base.OnModelCreating(modelBuilder);
        }
    }

Further technical details

EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 (1703)
IDE: Visual Studio 2017 15.5.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions