Skip to content
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

Entity Inserted and updated in one session, throws error #114

Open
sudipp opened this issue Sep 12, 2019 · 1 comment
Open

Entity Inserted and updated in one session, throws error #114

sudipp opened this issue Sep 12, 2019 · 1 comment

Comments

@sudipp
Copy link

sudipp commented Sep 12, 2019

I have a situation, where I insert an entity, and then update it on the same session context.
I get following error, after calling repo.Update(jobExecution);

please help.

{System.InvalidOperationException: The instance of entity type 'JobExecution' cannot be tracked because another instance with the same key value for {'JobExecutionSk'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

private readonly IUnitOfWork _UOW;
    public JobsController(IUnitOfWork uow)
    {
        _UOW = uow;
    }
	
    public async Task test()
    {
        var jobName = "test";
        var _repoJobExecution = _UOW.GetRepository<JobExecution>();
        var _repoJob = _UOW.GetRepository<Entities.Models.Job>();
        var jobExecution = new JobExecution
        {
            JobSk = 1,
            JobStatus = JobStatus.InProgress,
        };

        _repoJobExecution.Insert(jobExecution);
        await _UOW.SaveChangesAsync().ConfigureAwait(false);

        jobExecution = await _repoJobExecution.GetFirstOrDefaultAsync(
                predicate: j => j.Job.JobName.Equals(jobName)
                    && j.JobStatus == JobStatus.InProgress).ConfigureAwait(false);

        jobExecution.JobStatus = JobStatus.Completed;
        jobExecution.EndDatetime = DateTime.Now;
        _repoJobExecution.Update(jobExecution); //error thrown in this line
        await _UOW.SaveChangesAsync().ConfigureAwait(false);
    }
@liuhll
Copy link

liuhll commented Apr 2, 2021

me to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants