Alex D
Alex D

Reputation: 828

object's key information and cannot be modified

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

Upvotes: 0

Views: 953

Answers (3)

Jasy
Jasy

Reputation: 1

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.

Upvotes: 0

Equalsk
Equalsk

Reputation: 8214

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.

Upvotes: 3

Jorge Martins
Jorge Martins

Reputation: 1

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.

Upvotes: 0

Related Questions