Solved

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

Posted on 2010-09-07
5
2,031 Views
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.InsertOnSubmit(newAccount);
               else
               {
                  dataContext.Accounts.Attach(newAccount, this.accountToEdit);
               }

               dataContext.SubmitChanges();

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

               else
               {
                  [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
0
Comment
Question by:MickyMc
  • 3
  • 2
5 Comments
 
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.

Fernando
0
 

Author Comment

by:MickyMc
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
0
 
LVL 63

Accepted Solution

by:
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 http://msdn.microsoft.com/en-us/library/bb546187.aspx?appId=Dev10IDEF1&l=EN-US&k=k("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().

Fernando
0
 

Author Closing Comment

by:MickyMc
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
0
 
LVL 63

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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