?
Solved

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

Posted on 2010-09-07
5
Medium Priority
?
2,092 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
[X]
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
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 2000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

752 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