30666

The property 'Id' is part of the object's key information and cannot be modified

Question:

I have a sample web app that I am writing and im confused why i'm getting this

The property 'Id' is part of the object's key information and cannot be modified.

when i'm not updating the 'Id'?

Ok, so this is what I am trying to do.

I have 2 tables Topics and Posts and --

<ol><li>User creates a new topic it should add a topic record on the database</li> <li>Get the topic Id and set that Id to the Post's TopicId</li> <li>Get that post's Id and set it as Topic's LastPostId</li> </ol>

I'm re updating the post so whenever I need to display the last post made to the topic I don't need to do the "sort by date for all the post inside this topic". There should be a better way to do this..

When I debug i see that The topic category Id is being set as the Topic Id which i do not have in my update code.

// // insert new topic to database Topic topic = new Topic(); topic.CategoryId = int.Parse(RouteData.Values["id"].ToString()); topic.Title = postModel.Title; topicRepo.Add( topic ); topicRepo.Save(); // // insert post to database PostRepository postRepo = new PostRepository(); Post post = new Post(); post.TopicId = topic.Id; post.Body = postModel.Body; string strUserId = UserAccount.FormatUserName( User.Identity.Name ); post.CreatedByUser = strUserId; post.CreationDate = DateTime.Now; postRepo.Add( post ); postRepo.Save(); // *********************** // update topic last post // *********************** Topic updateTopic = topicRepo.GetTopic( topic.Id ); updateTopic.LastPostId = post.Id; TryUpdateModel( updateTopic ); if ( ModelState.IsValid ) topicRepo.Save();

Thanks!

Answer1:

This happens because TryUpdateModel tries to update all posted values. So if you have one or more values that you don't want updated, you have to do this manually.

For instance:

TryUpdateModel(updateTopic, "", null, new string[] { "Id" });

I'm guessing that one property named "ID" is also submitted into this action, causing the error.

Recommend

  • How to distribute an event to all nodes in a (Wildfly) cluster?
  • Making Cross Site Asynchronous HTTP Post from GWT Client
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • Compress a file with GZipStream while maintaining its meta-data
  • Marklogic : Query response time is very high
  • Why querying a date BC is changed to AD in Java?
  • Record samples being played with OpenAL
  • Cancel a live stream “fast motion” catch-up in Flash
  • Algorithm for a smudge tool?
  • Get one-time binding to work for ng-if
  • How do I change content of ComboFieldEditor?
  • Adding a button at the bottom of a table view
  • How can I send an e-mail from a vbs script
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Spring security and special characters
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • How do I fake an specific browser client when using Java's Net library?
  • Date difference with leap year
  • angularjs unit test when to use $rootScope.$new()
  • Volusion's generic SQL folder, functionality
  • Menu Color Fade on Hover with Jquery
  • ActionScript 2 vs ActionScript 3 performance
  • How to limit post in wp_query
  • Hazelcast - OperationTimeoutException
  • How to make Safari send if-modified-since header?
  • Adding custom controls to a full screen movie
  • To display the title for the current loaction in map in iphone
  • Traverse Array and Display in markup
  • AngularJs get employee from factory
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Memory offsets in inline assembly
  • Java static initializers and reflection
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Turn off referential integrity in Derby? is it possible?
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • UserPrincipal.Current returns apppool on IIS
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize