views:

439

answers:

1

I'm trying to use:

// this is a BreakHistory class from the ADO.NET Data Entity Model _entities
_entities.AddToBreakHistory(this);
_entities.SaveChanges();

and I'm getting "An entity object cannot be referenced by multiple instances of IEntityChangeTracker." error

I'm thinking that my update code:

_entities.ApplyPropertyChanges(this.EntityKey.EntitySetName, this);
_entities.SaveChanges();

and I'm thinking I'll probably get the same type of error.

Do I have to get the originalBreakHistory object as such?

var originalBreakHistory = (from bh in _entities.BreakHistorySet where bh.BreakHistoryID = id select bh).FirstOrDefault();

-- Updated at 4:40PM same day It seems that I may be having another problem that might be related. I'm adding the Manager entity object to the current BreakHistory entity since they're related and I'm getting this error: "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."

Could that error be because I'm using the same entity model, but calling "new" on the entity objects and loading them from the DB??

Here's some of my code:

protected void lstBreaks_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    var bh = new BreakHistory().CurrentBreakRequest(int.Parse(e.CommandArgument.ToString())).CurrentManagerIs(GetManagerEntityByUserName());

    switch (e.CommandName)
    {
        case "Approve":
            bh.Approve();
            break;
        case "Deny":
            bh.Deny();
            break;
        case "Push":
            bh.Push();
            break;
    }

    bh.Save();

    LoadBreakList();
}

The code for example on the Approve - bh.Approve() looks like this:

public BreakHistory Approve()
{
    this.DateApproved = DateTime.Now;
    this.ManagerApprove = CurrentManager;

    return this;
}

A manager can approve, deny, or push a request along and so the manager is related to the BreakHistory via a ManagerID.

A: 

You shouldn't have to use the new operator if you are retrieving an existing record. That may be what is hanging you up.

Make sure you are using the same DataContext object for all of your updates. It is difficult to move objects from one DataContext to another for numerous reasons.

Robert Harvey
I'm still learning Linq and the whole entity model structure. You were right, I was using two completely separate DataContexts.
kntcnrg