LINQ.. cannot attach a new object as identity of object cannot be changed

Posted on 2010-09-07
Last Modified: 2013-12-17
Hi there,  this looks handy ... but is wrecking my head. I fixed it but want to know why the orginal doesnt work.

I have an account object in the database and I edit it. On the save, I create a new object and if I'm creating a new one I do an InsertOnSublmit and if its an Edit then I attach, using the new one and the orginal.

I have a PK, which is autogenerated in the database. I have set its Auto-Sync to Onserted.

I can insert new accounts but when I edit them and save I get this error..

System.InvalidOperationException]      {"Value of member 'AccountID' of an object of type 'Account' changed.\r\nA member defining the identity of the object cannot be changed.\r\nConsider adding a new object with new identity and deleting the existing one instead."}      System.InvalidOperationException

When I instantiate a new Account and add the fields the AccountID is set to 0 as a default. So it seems when I do the attach and submit, that it thinks I'm trying to change the current PK to 0.

Here is the code...

Account newAccount = new Account();

newAccount.AccountName = AccountName;
newAccount.InitialBalance = Balance;
newAccount.AccountBaseCurrency  = Currency;
newAccount.AccountStatusTypeID = 1010;		// opened

using (var dataContext = DatabaseContext.Brokerage)
               if (this.accountToEdit == null)
                  dataContext.Accounts.Attach(newAccount, this.accountToEdit);


Open in new window

So the This.AccountTo Edit is the account I am currently editing.

If I put..
newAccount.AccountID = this.accountToEdit.AccountID;

Open in new window

before the attach then it works.  eg

                  [b]newAccount.AccountID = this.accountToEdit.AccountID;[/b]
                  dataContext.Accounts.Attach(newAccount, this.accountToEdit);

Open in new window

The accountID is a PK in and is defined in the DTML as ...

      [Column(Storage = "_AccountID", AutoSync = AutoSync.OnInsert, DbType = "BigInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
      public long AccountID { get; set; }

Open in new window

So I only want the accountID generated and returned on an Insert and not on an update.

Can anyone tell me what I am doing wrong...

thanks M
Question by:MickyMc
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 63

Expert Comment

by:Fernando Soto
ID: 33617536
Hi MickyMc;

You state the following, "if I'm creating a new one I do an InsertOnSublmit and if its an Edit then I attach, using the new one and the original.", If you have made a query and have a local copy of a record and you want to edit it you do NOT create a new record and then Attach, you just modify the original record and then do a SubmitChanges.


Author Comment

ID: 33618634
Hi Fernando,

yes indeed that makes perfect sense to me but why does the Attach method take a new object and the Orginal object. I thought this was a way to update the object in the database. Oh, I have inherited this code so the person that wrote it always created a new object, updated the details and off he went. He would decide if it was a new object or one that wass getting edited and then call InsertOnUpdate or Attach and use the new object.

He brings the orginal back using the SingleOrDefault method, and passes this with the new object on the Edit, by attaching both..

regards Mick
LVL 63

Accepted Solution

Fernando Soto earned 500 total points
ID: 33619171
Hi MickyMc;

To your statement, "why does the Attach method take a new object and the Original object. I thought this was a way to update the object in the database", The following comes from the Microsoft documentation, "When you serialize entity objects such as Customers or Orders to a client over a network, those entities are detached from their data context. The data context no longer tracks their changes or their associations with other objects. This is not an issue as long as the clients are only reading the data. It is also relatively simple to enable clients to add new rows to a database. However, if your application requires that clients be able to update or delete data, then you must attach the entities to a new data context before you call DataContext.SubmitChanges. In addition, if you are using an optimistic concurrency check with original values, then you will also need a way to provide the database both the original entity and the entity as modified. The Attach methods are provided to enable you to put entities into a new data context after they have been detached." found at this link"SYSTEM.DATA.LINQ.TABLE`1.ATTACH");k(TargetFrameworkMoniker-".NETFRAMEWORK&k=VERSION=V4.0");k(DevLang-VB)&rd=true. When the documentation talks about, "When you serialize entity objects such as Customers or Orders to a client over a network", it is saying that an application on one system transmits the entities over the network to another system and you want to do CRUD on the entities you need to Attach the entities to a new DataContext and that is done with the Attach method. When it talks about Original the data context keeps the entity original states as well as the current state and that is the original it is talking about.

If you are not doing this in a networking situation as stated above you should be using the format  of DataContext.TableName.InsertOnSubmit to add new entities and just modifying existing entities to modify its values and of course DataContext.SubmitChanges().


Author Closing Comment

ID: 33624544
thanks Fernando... yes you are correct, I just want to add new and update existing so it makes sense. I can see now how its used for concurrency by passing the orginal so happy out... regard Mick
LVL 63

Expert Comment

by:Fernando Soto
ID: 33629558
Not a problem Mick, glad to help.  ;=)

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question