object's key information and cannot be modified

object example
object grammar
5 examples of object
objects examples
object synonym
object in programming
object javascript
objects list

I am trying to duplicate currentAppointment to appointment. My issue is that currentAppointment has a GUID. When I try to create a new duplicate appointment I get the following error. 'The property 'AppID' is part of the object's key information and cannot be modified. '.

This completely makes sense why I am getting this error and I know I can get around it by going field by field down and match them up (32 fields) but I would like to know if there is a way to give 'appointment' a new guid without going field by field.

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

So as mentioned in my comment you should read about Reference Types vs Value Types to understand why you get this error.

In short when you say Appointment appointment = currentAppointment; you're kind of creating a link from appointment to the same object that currentAppointment represents. When you change a property on either, it will change on the other.

Entity Framework has a way to map one class onto another like this:

// Get current Appointment
var currentAppointment = db.Appointments.Find(id);

// Make a brand new Appointment
var appointment = new Appointment();

// Map one to the other
db.Entry(currentAppointment).CurrentValues.SetValues(appointment);

// Change the ID
appointment.AppID = Guid.NewGuid();

// Add to database
db.Appointments.Add(appointment);

// Whatever else happens

// Save
db.SaveChanges();

You may want to do some work to avoid collision as there's a chance that the GUID for the appointment could already exist.

Personally I'd recommend having the database column for AppId set as an identifier that generates the GUID itself. Then your object in C# just has a null ID when added, the rest is done at the database level.

Object, a noun or noun phrase governed by an active transitive verb or by a preposition. Object definition, anything that is visible or tangible and is relatively stable in form. See more.

With that error, I think your AppID, is your primary Key. So if this is a primary key, your Guid.NewGuid(); can return a value wich already exist in database. So you can save that in a variable and validade if exist any appointment with that key, if not add, if exist, you car generate another.

OBJECT, a data construct that provides a description of something that may be used by a computer (such as a processor, a peripheral, a document, or a data set) and defines its status, its method of operation, and how it interacts with other objects. object Grammatical objects are nouns or pronouns that complete the meaning of verbs and prepositions. Additionally, almost any group of words that functions as a noun can be an object, such as noun phrases, noun clauses, gerunds, and infinitives.

To create new objects in a loop, you can use code like this:

foreach (Array element in empList)
{
  objEmp= new Employee(); // this is the line i wrote to repair error
  objEmp.Name= "EmpName";
  objEmp.Mobile= "012345678942";
  int nextID=getnextIDFunction();
  objEmp.PKID = nextVisitParamID;
  bllEmp.Save(objEmp);
}

The important part here is that you create a new Object each time. The values that you add to the new Object would retrieved from the old.

Object, say something to express one's disapproval of or disagreement with something. object definition: 1. a thing that you can see or touch but that is not usually a living animal, plant, or person: 2…. Learn more.

Object, a thing that can be seen, held, or touched, usually not a living thing: Distant objects look blurry to me. object noun [  39 synonyms of object from the Merriam-Webster Thesaurus, plus 104 related words, definitions, and antonyms. Find another word for object. Object: something material that can be perceived by the senses.

Object (grammar), Changes to the Object prototype object are seen by all objects through prototype chaining, unless the properties and methods subject to those  An object is one of the five major elements of clause structure. The other four are subject, verb, adjunct and complement. Objects are typically noun phrases (a noun or pronoun and any dependent words before or after it). Objects normally follow the verb in a clause:

OBJECT (noun) definition and synonyms, Object (philosophy), a thing, being, or concept. Entity, something that is tangible and within the grasp of the senses. Object (abstract), an object which does not exist at any particular time or place. Physical object, an identifiable collection of matter. An object is a noun (or pronoun) that is governed by a verb or a preposition. There are three kinds of object: Direct Object (e.g., I know him.) Object of a Preposition (e.g., Sit with them.)

Comments
  • You need to read about reference types (and probably value types too). You get the error because appointment is just a pointer to the same object represented by currentAppointment, if you change one you change the other. What is db? Is it Entity Framework or something else?
  • Entity Framework
  • You could consider using AsNoTracking to accomplish this, perhaps. stackoverflow.com/questions/15308747/…