属性“Id”是对象的关键信息的一部分,不能修改 [英] The property 'Id' is part of the object's key information and cannot be modified

查看:196
本文介绍了属性“Id”是对象的关键信息的一部分,不能修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



属性Id是对象的关键信息的一部分,不能修改。



当我不更新'Id'?



好的,所以这是我想做什么



我有2个表格主题和帖子 -


  1. 用户创建一个新主题应该在数据库中添加一个主题记录

  2. 获取主题ID并将该ID设置为帖子的主题标识

  3. 获取该帖子的ID和将其设置为主题的LastPostId

我正在更新帖子,所以每当我需要显示最后一个帖子的主题
我不需要按照这个主题中的所有帖子按日期排序。
应该有一个更好的方式来做这个..



当我调试我看到
主题类别Id被设置为主题Id
我没有在我的更新代码。

  // 
//插入新主题到数据库

主题topic = new Topic();

topic.CategoryId = int.Parse(RouteData.Values [id]。ToString());
topic.Title = postModel.Title;

topicRepo.Add(topic);
topicRepo.Save();

//
//插入到数据库
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();

// ***********************
//更新主题最后发布
// * **********************
主题updateTopic = topicRepo.GetTopic(topic.Id);
updateTopic.LastPostId = post.Id;

TryUpdateModel(updateTopic);
if(ModelState.IsValid)
topicRepo.Save();

谢谢!

解决方案

这是因为TryUpdateModel尝试更新所有发布的值。因此,如果您有一个或多个不希望更新的值,则必须手动执行此操作。例如:

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

我猜测一个名为ID的属性也被提交到此操作中,导致错误。


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 --

  1. User creates a new topic it should add a topic record on the database
  2. Get the topic Id and set that Id to the Post's TopicId
  3. Get that post's Id and set it as Topic's LastPostId

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!

解决方案

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.

这篇关于属性“Id”是对象的关键信息的一部分,不能修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆