EF Core 2 throws exception on SaveChanges, after first update for related entities

c# ef-core-2.0 entity-framework-core

Question

hi :) i ran in to a problem with editing/updating entries in db which has related entities. i am able to edit any entry once, and after that i am not able to update any dependent entity wich has the same principal entity, as the entity i already modified

i have spend last 5 days or so, trying to solve this. all the advice that i have found on the net, did not work :(

u can see the project @: https://github.com/nedimbih/WorkNotes
problematic part is this:

public partial class Work
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }


    public virtual Worker Worker { get; set; }

    public DateTime Date { get; set; }
    public TimeSpan StartingTime { get; set; }
    public float Duration { get; set; }
    public string Note { get; set; }
}

and

public partial class Worker : IdentityUser
{

    public Worker() => WorksDoneByWorker = new HashSet<Work>();


    public virtual  ICollection<Work> WorksDoneByWorker { get; set; }
}

In WorkRepository.cs i have this code

 internal int Update(Work input)
    {

        Work workInDb = _context.WorkList
                                .Include(w => w.Worker)
                                .FirstOrDefault(w => w.Id == input.Id);

        if (workInDb != null)
        // v.1
        // {workInDb = input;}
        // v.2
        {
            workInDb.Note = input.Note;
            workInDb.StartingTime = input.StartingTime;
            workInDb.Duration = input.Duration;
            workInDb.Date = input.Date;
            workInDb.Worker = input.Worker;
        }
        int savedEntries;
        try
        {
            savedEntries = _context.SaveChanges();
        }
        catch (Exception)
        {
            savedEntries = 0;
            // used as a flag. Calling code acts upon it
        }
        return savedEntries;
    }

i can make updates on work entries only once, for a given worker.
after i edit/update one work entry (savedEntries holds value 2), i can no longer update any entry with same worker as updated entry. i get exception on SaveChanges saying that Worker with same id is already being tracked.
if i turn of line workInDb.Worker=input.Worker then it saves, but that is not functionality that i need.
if i turn on v.1 code instead of v.2 i get no exception but SaveChanges does nothing.
count of modified entries (in context) is 0 in both cases.

thx :)

1
0
3/13/2018 6:06:23 PM

Accepted Answer

I don't think your model is right. Try this:

public partial class Work
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public int WorkerId { get; set; }
public Worker Worker { get; set; }

public DateTime Date { get; set; }
public TimeSpan StartingTime { get; set; }
public float Duration { get; set; }
public string Note { get; set; }
}

and the instead of setting the worker set its Id like this:

 workInDb.WorkerId = input.Worker.Id;

This will prevent EF of trying to create and store a new Worker with the same Id, but instead in adding a relation to the existing worker.

1
2/28/2018 11:33:27 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