-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Stop fixing up to Deleted entities #28249
Comments
@qslcna EF Core 5.0 is no longer supported. Please update to EF Core 6.0 and report back if yo are still seeing the issue. |
@ajcvickers I tried EF Core 6.0.6. It has same issue. |
I want achive like this. [Fact]
public void Test1()
{
var ctx = CreateContext();
var post = ctx.Posts.Include(x => x.PostTags).SingleOrDefault(x => x.Id == 1);
if (post != null)
{
post.PostTags.RemoveAt(1);
}
//ctx.ChangeTracker.DetectChanges();
//Debug.WriteLine(ctx.ChangeTracker.DebugView.LongView);
// But In my real project. I call _context.ChangeTracker.Entries here. Which will trigger DetectChanges.
var tag = ctx.Tags.Include(x => x.PostTags).ToList();
Debug.WriteLine(ctx.ChangeTracker.DebugView.LongView);
ctx.ChangeTracker.DetectChanges();
Debug.WriteLine(ctx.ChangeTracker.DebugView.LongView);
ctx.SaveChanges();
}
|
Note for triage: EF is fixing up navigations to Deleted entities when queries are executed. I think there are cases where this is desirable, but it'll take some investigation for me to remember. |
@ajcvickers EF always fixing up added entities. why it behaves differently when handle deleted entities? |
@ajcvickers How is the investigation? |
@qslcna Are you saying that you want fixup to Deleted entities happen or not happen when a query is executed? |
@ajcvickers I want it happend. The point is in many to many relationship, remove form |
@qslcna You need to call See the docs on change detection: https://docs.microsoft.com/en-us/ef/core/change-tracking/change-detection |
I'm confused for the meaning of fixup now.
I removed Then I query tags;
In this scenario fix up happend or not? |
You need to call |
Please check the original code (at the top of page). I did that. But the result of query tags still has the entity |
Have I made it clear? |
Yes if deleted entities no longer in the graph, it should disapper form any navigation collections. |
@ajcvickers the fisrt part ( description about my question ) of original post has updated. Anxiously waiting for your reply. I've been struggling with this issue for a long time |
@qslcna Currently, executing a query causes the queried entities to be connected to any related Deleted entities, and vice-versa. I agree that this may not be the desired behavior. This is something we will investigate. However, that investigation will happen in due course as part of our normal work. |
public class Receipt
{
public DateTime Date { get; set; }
public int? CustomerId { get; set; }
public int? SupplierId { get; set; }
public decimal Total { get; set; }
public virtual Customer Customer { get; set; }
public virtual Supplier Supplier { get; set; }
public virtual List<WriteOff> WriteOffs { get; set; }
}
public class SaleOut
{
public DateTime Date { get; set; }
public int? CustomerId { get; set; }
public int? SupplierId { get; set; }
public decimal Total { get; private set; }
public decimal Progress { get; private set; }
public virtual Supplier Supplier { get; set; }
public virtual Customer Customer { get; set; }
public virtual List<SaleOutItem> Items { get; set; }
public virtual List<WriteOff> WriteOffs { get; private set; }
public void WriteOff()
{
var writeOffsTotal = this.WriteOffs.Sum(x => x.Amount);
if (writeOffsTotal < 0 || writeOffsTotal > this.Total)
{
throw new Exception();
}
this.Progress = writeOffsTotal;
}
}
public class WriteOff
{
public decimal Amount { get; set; }
public int TradeId { get; set; }
public int OwnerId { get; set; }
public virtual SaleOut Trade { get; set; }
public virtual Receipt Owner { get; set; }
} Consider the situation, If
if the query result still connected to Do you have any advice on this. |
@qslcna I'm afraid I find it very hard to understand the details of what you are trying to do, and hence don't really have any advice. |
|
By reading this post relationship-changes. In many-to-many relationships, I consider when removing join entities
PostTags
frompost.PostTags
the removed entity will also disapper fromtag.PostTags
If query tags before
ctx.ChangeTracker.DetectChanges()
it performs as expected.However call
ctx.ChangeTracker.DetectChanges()
before query tags, thetag{id=2}
's navigation propertyPostTags
still has{PostId: 1, TagId: 2}
which state is deleted.It dosen't disapper although I call
ctx.ChangeTracker.DetectChanges()
againhow to make DetectChanges has consistent result, no matter what order of invocation.
Entitis
UnitTest1.cs
Include provider and version information
EF Core version:5.0.12
Database provider: Pomelo.EntityFrameworkCore.MySql
Target framework:NET 5.0
Operating system:
IDE: vscode latest
The text was updated successfully, but these errors were encountered: