Solved

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

Posted on 2010-09-07
5
1,996 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 62

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 62

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 62

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now