• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2120
  • Last Modified:

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

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
MickyMc
Asked:
MickyMc
  • 3
  • 2
1 Solution
 
Fernando SotoCommented:
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
 
MickyMcAuthor Commented:
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
 
Fernando SotoCommented:
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
 
MickyMcAuthor Commented:
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
 
Fernando SotoCommented:
Not a problem Mick, glad to help.  ;=)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now