Repository navigation
Closed as duplicate
Labels
Description
When I Update an entity's owned type to null, then do Detect Changes. after that read the navigation property, it throws InvalidOperationException: "A tracking query projects owned entity without corresponding owner in result. Owned entities cannot be tracked without their owner. Either include the owner entity in the result or make query non-tracking using AsNoTracking().";
If I set the naviation poperty to another instance which is not null,I can read it successfully.
by the way.when when lazy loading is not used, I can read it as null well.
Steps to reproduce
public class Order
{
public int Id { get; set; }
public StreetAddress? ShippingAddress { get; set; }
}
[Owned]
public class StreetAddress
{
public string Street { get; set; }
public string City { get; set; }
}
public class TestDbContext : DbContext
{
public DbSet<Order> Orders {get;set;}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseLazyLoadingProxies();
}
}
using (var context = new TestDbContext())
{
// seed order with non-null ShippingAddress.
context.Orders.Add(new Order {Id = 1, ShippingAddress = new ShippingAddress {Street ="a", City = "b"}});
context.SaveChanges();
}
// update ShippingAddress to null will throw the InvalidOperationException.
using (var context = new TestDbContext())
{
var order = context.Orders.First();
// set ShippingAddress to null.
order.ShippingAddress = null;
context.Attach(order);
context.Update(order);
Console.WriteLine(order.ShippingAddress); // prints null.
context.ChangeTracker.DetectChanges();
Console.WriteLine(order.ShippingAddress); // will throw an exception.
}
// update ShippingAddress to non-null will not throw the InvalidOperationException.
using (var context = new TestDbContext())
{
var order = context.Orders.First();
// set ShippingAddress to non-null.
order.ShippingAddress = new ShippingAddress{Street ="c", City = "d"};
context.Attach(order);
context.Update(order);
Console.WriteLine(order.ShippingAddress); // prints updated ShippingAddress.
context.ChangeTracker.DetectChanges();
Console.WriteLine(order.ShippingAddress); // prints updated ShippingAddress without the invalid operation exception
}Further technical details
EF Core version:3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: netcoreapp3.1
Operating system: windows 10
IDE: Visual Studio 2019 16.3
Activity
ajcvickers commentedon Feb 12, 2020
@xkaede I have not been able to reproduce the exception you are seeing--my code is below. Please post a small, runnable project or a complete code listing (like below) that reproduces the behavior you are seeing.
Also, it's worth noting that in the code above the Attach and Update calls are at best unnecessary, and potentially not correct. Since the code is doing a tracking query there is no value in then explicitly tracking with Attach or Update. Also, calling Update after Attach is usually incorrect. I wrote a couple of blog posts on this that might be useful: https://blog.oneunicorn.com/2020/01/17/dontcallupdate/ and https://blog.oneunicorn.com/2020/01/18/docallupdate/.
xkaede commentedon Feb 13, 2020
hi @ajcvickers
Thanks for your detailed explanation.
In the code above you missed the lazy loding configuration. The issues only happens when lazy loading is turned on.
and I readed your blog posts,It is useful for me.I didn't knwon attach and update well.
but let me explain.
I used Attach and Update in the code before in order to simplify the problem.The real scene is that I want to collect the changed entities which is Added and Modified and Deleted before SaveChanges.then send them to a message bus.below is the code.
I tried to disable the AutoDetectChanges,but the state of the entities is all unchanged.
Here's the complete code.
ajcvickers commentedon Feb 17, 2020
@xkaede Try disabling lazy-loading while doing your processing of the tracked entities. Something like:
Team: Lazy-loading of an owned entity throws:
[-]Null Owned Type throws exception after Detect Changes when lazy loading is on[/-][+]Lazy-loading of an owned entity throws[/+]xkaede commentedon Feb 18, 2020
@ajcvickers Disabling lazy-loading works, but I need to lazy load entity's data when I map it to a dto, so If disable lazy-loading, I will miss the data.
ajcvickers commentedon Feb 18, 2020
Note for team: probably covered by #12462--stop lazy-loading in this case.
ajcvickers commentedon Feb 19, 2020
@AndriySvyryd Would you agree that lazy loading of any part of an aggregate should be a no-op? The reasoning being that an aggregate should always be fully loaded. This means that even if a reference to an owned entity is null after the owner has been attached, then we should still never attempt to load it?
/cc @smitpatel
ajcvickers commentedon Feb 19, 2020
@xkaede I'm not suggesting that you disable it always, but rather that you disable it while process the entities to generate the events. (This case looks a lot like auditing entity states. In those cases you pretty much always need to turn off lazy loading anyway because otherwise your whole database graph would be loaded into memory.)
AndriySvyryd commentedon Feb 19, 2020
@ajcvickers That only applies for tracked entities, for non-tracked we allow to query owned entities without the owner.
ajcvickers commentedon Feb 19, 2020
@AndriySvyryd Good point. Although lazy loading doesn't currently work for non-tracked entities anyway.
xkaede commentedon Feb 20, 2020
@ajcvickers Thank you very much.
Follow your method.I disable the lazy loading and do Explicitly Loading before map entities to generate the events like below. and it works.
ajcvickers commentedon Feb 21, 2020
Team notes: The fix for #12462 will stop this throwing because the navigation will no longer be configured to lazy-load.
That being said, if loading of owned entity is explicitly triggered through the ILazyLoader mechanism, then in this case (owner with null, non-loaded owned) then this will still throw. If many people hit this then we could consider a better exception.
Xriuk commentedon Feb 24, 2023
Is there a reason for this? Why can't aggregates be lazy loaded and tracked? I my entity OwnsMany aggregates and I don't want to eager load them all but only where needed why can't this happen?
For example currently I have my
Products entities which are owned by the correspondingOrder, which makes sense logically (at least to me). But I may need to not load the products if I'm only looking at orders, and when looking at a particular order when I need to get info about its products they get lazy loaded, only when needed.ajcvickers commentedon Feb 24, 2023
@Xriuk An aggregate is used as a single unit, always loaded together. If you work with an order without its products, then it's not an aggregate.