如何更新实体实体框架中的属性 [英] How do I update a property in my entity entity framework

查看:93
本文介绍了如何更新实体实体框架中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Guys



我目前遇到以下问题。



我正在使用使用mvc的实体框架,我正在尝试根据模型保存一些数据。



所以这是目前从用户角度进行的过程

用户创建记录

保存记录
并且能够通过一次或两次更新复制记录并保存为新记录。 />


我遇到的问题是,当他们试图保存第一条记录时,有一种生成记录密钥的方法,记录保存没有问题。如果用户在生成密钥后尝试复制并保存,我尝试添加记录,我收到以下错误该属性是对象的关键信息的一部分,无法修改



我尝试了什么:



这是我目前的代码



Hi Guys

I am currently having an issue with the following.

I am using the entity framework with mvc and i am trying to save some data according to the model.

So this is the process that currently takes place from a user perspective
Users create a record
Save a record
and have the ability to copy the record with one or two updates and save as a new record.

Issue i am having is when they try to save the first record there is a method which generates the key for the record, the record saves with no issues. if the user tries to copy and save once the key is generated and i try to add the record i am getting the following error "The property is part of the object's key information and cannot be modified"

What I have tried:

This is my current code

public void Add(Customer entity)
        {
            
            if (entity.PortalId == new Guid("{00000000-0000-0000-0000-000000000000}"))
            {
         
                entity.PortalId = Guid.NewGuid();
            }
            this.DbContext.Suppliers.Add(entity);
         
        }

推荐答案

尝试删除if语句,只生成一个新ID。
Try removing the if statement and just generate a new ID.


这篇关于如何更新实体实体框架中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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