Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CosmosDB] - EFCore is failing to save when same object is assigned to entity properties twice #28495

Closed
Karthigeyan73 opened this issue Jul 22, 2022 · 1 comment

Comments

@Karthigeyan73
Copy link

Karthigeyan73 commented Jul 22, 2022

I am trying to save an entity by assigning same objects to two properties, but it fails to save and throws error.
Database: Azure CosmosDB

With the below code, I am not able to save:

    Order order = new Order() { id = Guid.NewGuid().ToString(), Description = "Order#1" };
    Product product = new Product() { SKU = "one", Description = "this is product one" };

    order.Product1 = product;
    order.Product2 = product;

stack traces

System.InvalidOperationException: Unable to track an entity of type 'Order.Product2#Product' because its primary key property 'Orderid' is null.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableKeyIdentityMap`1.Add(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.SetEntityState(InternalEntityEntry entry, EntityState entityState)
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Add(TEntity entity)
   at CosmosDbApi.Controllers.OrdersController.PostOrder(Order order1) in D:\Learnings\Cosmos DB\CosmosClient\CosmosDbApi\Controllers\OrdersController.cs:line 99

With the below code, I am able to save:

    Order order = new Order() { id = Guid.NewGuid().ToString(), Description = "Order#1" };
    Product product1 = new Product() { SKU = "one", Description = "this is product one" };
    Product product2 = new Product() { SKU = "one", Description = "this is product one" };

    order.Product1 = product1;
    order.Product2 = product2;

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022

@ajcvickers
Copy link
Contributor

Duplicate of #12345

@ajcvickers ajcvickers marked this as a duplicate of #12345 Jul 23, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants