views:

191

answers:

1

Hello, I'm currently using the repository pattern in an ASP.Net MVC project and have the following code:

    partial class Timesheet : ITimesheet
        {
            TimeSystemDBEntities _db;

            public Timesheet()
            {
                _db = new TimeSystemDBEntities();
            }
            .
            .
             //More Methods
            .
            .

            //Save WorkWeek to database
            private void SaveWorkWeek(WorkWeek wk)
                {
                    //Creating a new test context in attempt to resolve
                    //Error: “An entity object cannot be referenced by multiple instances of IEntityChangeTracker"
                    var testContext = new TimeSystemDBEntities();
                    var newWorkWeek = new WorkWeek();
                    var newStudent = new Student();
                    //Copy contents of wk to newWorkWeek
                    newWorkWeek.Students = newStudent.GetStudent(wk.Students.id);
                    newWorkWeek.MonDate = wk.MonDate;
                    newWorkWeek.MonHours = wk.MonHours;
                    newWorkWeek.TuesDate = wk.TuesDate;
                    newWorkWeek.TuesHours = wk.TuesHours;
                    newWorkWeek.WedDate = wk.WedDate;
                    newWorkWeek.WedHours = wk.WedHours;
                    newWorkWeek.ThursDate = wk.ThursDate;
                    newWorkWeek.ThursHours = wk.ThursHours;
                    newWorkWeek.FriDate = wk.FriDate;
                    newWorkWeek.FriHours = wk.FriHours;
                    newWorkWeek.TempSave = wk.TempSave;

                    testContext.AddToWorkWeek(newWorkWeek); //Exception thrown here
                    testContext.SaveChanges();

                    //Also tried
                    //_db.AddToWorkWeek(newWorkWeek); //Exception thrown here
                    //_db.SaveChanges();

                    //Also tried
                    //_db.detach(wk): //Different exception thrown here: Says it's not in ObjectStateManager
                    //_db.AddToWorkWeek(newWorkWeek); 
                    //_db.SaveChanges();

                    //Also tried
                    //_db.detach(wk.Students): //Different exception thrown here: Says it's not in ObjectStateManager
                    //_db.AddToWorkWeek(newWorkWeek); 
                    //_db.SaveChanges();

                }

    }

The Student and WorkWeek classes are in a 1-to-Many relationship, respectively. I have tried everything from detaching to creating a completely new context as in the example code above and cannot get the object to save. When I try and detach it basically says it's not in the ObjectStateManager, which I interpret as there's nothing to detach, which ,if true, seems to contradict the original error. I'm not sure what's going on, any help is appreciated.

A: 

@Craig, thanks I had actually already looked at those links and their solutions and attempted to implement them with no success.

The following link provided me with the solution to my problem, also has other very useful information about other miscellaneous issues that have to do with the Entity Framework.

Entity Framework Gotchas

kingrichard2005