【发布时间】:2018-09-13 20:01:00
【问题描述】:
我正在尝试将 currentAppointment 复制到约会。我的问题是 currentAppointment 有一个 GUID。当我尝试创建一个新的重复约会时,我收到以下错误。 '属性'AppID'是对象的关键信息的一部分,不能修改。 '。
这完全有道理,为什么我会收到此错误,我知道我可以通过逐个字段向下匹配它们(32 个字段)来解决它,但我想知道是否有办法提供“约会” ' 一个新的向导,无需逐个字段。
Appointment currentAppointment = db.Appointments.Find(id);
Appointment appointment = currentAppointment;
appointment.AppID = Guid.NewGuid(); (where I get the error since I already have a guid from currentAppointment but would like appointment to have a new one.)
appointment.AgentID = 1;
appointment.StatusID = 13;
db.Appointments.Add(appointment);
【问题讨论】:
-
你需要阅读reference types(可能还有value types)。您会收到错误,因为
appointment只是指向currentAppointment所表示的同一对象的指针,如果您更改一个,则更改另一个。db是什么?是实体框架还是别的什么? -
实体框架
-
您或许可以考虑使用
AsNoTracking来完成此操作。 stackoverflow.com/questions/15308747/…
标签: c# .net asp.net-mvc