How do edit an entity (with AsNoTracking) after add

asp.net-core entity-framework-core

Question

With the following code

public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
        ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
    }


public class Index3Model : PageModel
{
    public Index3Model(MyDbContext myDbContext)
    {
        MyDbContext = myDbContext;
    }

    public MyDbContext MyDbContext { get; }

    public void OnGet()
    {
        var p = new Personen();
        p.Name = "PersonName";
        p.Adresse = new Adressen() { Name = "AdressName" };
        MyDbContext.Personen.Add(p);
        MyDbContext.SaveChanges();

        // Do something ....

        var p1 = MyDbContext.Personen.Where(a => a.Name == "PersonName").Include(a => a.Adresse).First();
        p1.Name += "Test";
        p1.Adresse.Name += "Test";
        MyDbContext.Personen.Update(p1);
        MyDbContext.SaveChanges();
    }
}

i get the following exception:

InvalidOperationException: The instance of entity type 'Personen' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

I think i understand the problem, but how can i resolve it?

Edit: The second part of my code is in an other function, therefor i cant use "p" again:

public Index3Model(MyDbContext myDbContext)
    {
        MyDbContext = myDbContext;
    }

    public MyDbContext MyDbContext { get; }

    public void OnGet()
    {
        var p = new Personen();
        p.Name = "PersonName";
        p.Adresse = new Adressen() { Name = "AdressName" };
        MyDbContext.Personen.Add(p);
        MyDbContext.SaveChanges();

        OtherFunction();
    }

    public void OtherFunction()
    {
        // Do something ....
        var p1 = MyDbContext.Personen.Where(a => a.Name == "PersonName").Include(a => a.Adresse).First();
        p1.Name += "Test";
        p1.Adresse.Name += "Test";
        MyDbContext.Personen.Update(p1);
        MyDbContext.SaveChanges();
    }

When i detach "p" after the Add method with

MyDbContext.Entry(p).State = EntityState.Detached;

the adress object is still in the context. I understand that it is possible to detatch the "Adress" object too, but i am looking for a general solution for this problem. I think in a little bit more complex scenarios, this error occurse very often.

Edit 2: My problem is, that i dont want to pass "p" in this function, because i call the second function from many other places in my project and in this places i dont have a object "p".

And i think there are many other scenarios, where i dont know if a object or maybe a list of objects contains entities, which are in the context, but i dont know it.

1
0
12/9/2019 8:44:48 AM

Popular Answer

It works when I use below code:

public void OnGet()
{
    var p = new Personen();
    p.Name = "PersonName";
    p.Adresse = new Adressen() { Name = "AdressName" };
    MyDbContext.Personen.Add(p);
    MyDbContext.SaveChanges();

    MyDbContext.Entry(p).State = EntityState.Detached;

    OtherFunction();
}

public void OtherFunction()
{
    // Do something ....
    var p1 = _context.Persons.Where(a => a.Name == "PersonName").Include(a => a.Address).First();
    p1.Name += "Test";
    p1.Adresse.Name += "Test";
    MyDbContext.Personen.Update(p1);
    MyDbContext.SaveChanges();
}
0
12/9/2019 9:47:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow