-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Changes in parent owned collection cause unexpected behavior in nested owned collection #27918
Comments
I'm not sure if this is a separate issue, but I encountered another issue which is very similar to this. Lets say you have an ICollection<Book> Books in your DbContext, and your Book class has an owned ICollection<Page> Pages, now imagine this scenario:
This will throw an InvalidOperationException stating that Page.Id is readonly and has been changed or marked as changed. Two things will solve this:
Curious if this is the same issue/root cause or if it's a different issue entirely, or even expected behavior... Hard to know sometimes with the Cosmos DB provider. |
Actually once I wrote it out fully I realized that it's probably not correct to re-use the context to call SaveChangesAsync multiple times. Correct me if I'm wrong but I'm guessing you will say that the following code is expected to fail (it does): using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
class TestContext : DbContext
{
public TestContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseCosmos("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "TestDb");
}
public DbSet<Book> Books { get; set; }
}
class Book
{
public int Id { get; set; }
public ICollection<Page> Pages { get; set; }
}
[Owned]
class Page
{
public string Text { get; set; } = "";
}
class Program
{
static async Task Main(string[] args)
{
using (var context = new TestContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Books.Add(
new Book()
{
Id = 1,
Pages = new List<Page>() { new Page() }
});
context.Books.Add(
new Book()
{
Id = 2,
Pages = new List<Page>() { new Page() }
});
await context.SaveChangesAsync();
}
using(var context = new TestContext())
{
var book1 = context.Books.Where(b => b.Id == 1).First();
book1.Pages.Add(new Page());
await context.SaveChangesAsync();
var book2 = context.Books.Where(b => b.Id == 2).First();
book2.Pages.Add(new Page());
await context.SaveChangesAsync();
}
}
} |
@brasky Reusing the context is not recommended, but it should work fine once the issue is fixed |
@AndriySvyryd I was the one that you opened this bug for (Minh Nguyen), I am wondering if this is related to nested properties not getting saved, i.e. if in the family/kid/toy model, if we add a new nested property to toy
adding this new property and updating it will not persist |
In the following scenario the
Toy
is not deleted:Model
Additionally if
Toy.ToyId
is removed the following exception is thrown:InvalidOperationException
Could be related to #28600
The text was updated successfully, but these errors were encountered: