1

I'm new to Entity Framework so please bear with me.

I have these entities in my DAL

    public class TimeSheet
    {
        [Key]
        public int TimeSheetID { get; set; }
        public DateTime SignIn { get; set; }
        public DateTime SignOut { get; set; }

        public virtual ICollection<TimeSheetDetail> TimeSheetDetails { get; set; }
    }

    public class TimeSheetDetail
    {
        [Key]
        public int TimeSheetDetailID { get; set; }
        public string Description { get; set; }
    }

And I got the following error (Attaching an entity of 'Model' failed because another entity of the same type already has the same primary key value) when trying to Update the record, my update code below :

public class TimeSheetService : ITimeSheetService
{
    private readonly IRepository<TimeSheet> _repo;
    private readonly IRepository<TimeSheetDetail> _repo2;

    public void EditTimeSheet(TimeSheet obj)
    {
        _repo.Update(obj); //Update TimeSheet <-- error here

        //loop TimeSheetDetails
        foreach (var item in obj.TimeSheetDetails)
        {
            //Insert
            if (_repo2.GetById(item.TimeSheetDetailID) == null)
            {
                _repo2.Insert(item);
            }
            //Update
            else
            {
                _repo2.Update(item);
            }
        }

        foreach (var item in _repo.GetById(obj.TimeSheetID).TimeSheetDetails)
        {
            //Delete
            if (!obj.TimeSheetDetails.Any(w => w.TimeSheetDetailID == item.TimeSheetDetailID))
            {
                _repo2.Delete(item.TimeSheetDetailID);
            }
        }
    }
}

My generic repository :

    public void Insert(T obj)
    {
        _dbSet.Add(obj);
    }

    public void Update(T obj)
    {
        _dbSet.Attach(obj);
        _dbContext.Entry(obj).State = EntityState.Modified;
    }

    public void Delete(T obj)
    {
        _dbSet.Remove(obj);
    }

I've looked around and got a mixed answer. So my question is :

  1. How to achieve this?
  2. Is this an acceptable way to do this? Is there any other simpler way?

Any help will be appreciated.

tickwave
  • 3,335
  • 6
  • 41
  • 82
  • 1
    `_dbSet.Attach(obj);` will attach `obj` to your DbContext, and if this object is already attached to the context, an exception will be thrown because you cannot have more than one entity with the same primary key, which is the error you're seeing. – Ahmad Ibrahim Jul 09 '15 at 03:30
  • So is there any solution to this problem? It's true that I've already attach `TimeSheet` object, but the EntityFramework by default will not recognize the collection inside `TimeSheet` object. – tickwave Jul 09 '15 at 03:36
  • You might have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:39

0 Answers0