I'm working with Entity Framework using a Oracle database. What I'm trying to do is to save a new Entity using a old entity as base. I take all the properties of the old entity and take them to a new one, this with the objective of avoid problems with duplication of entities or multiple references.
I'm doing something like this:
List < PPL_FAMILIAR_RESPONSABLE > familiares = new List < PPL_FAMILIAR_RESPONSABLE > ();
foreach(PPL_FAMILIAR_RESPONSABLE fr in registro.PPL_FAMILIAR_RESPONSABLE) {
familiares.Add(new PPL_FAMILIAR_RESPONSABLE {
FR_NOMBRE = fr.FR_NOMBRE,
FR_APPATERNO = fr.FR_APPATERNO,
FR_APMATERNO = fr.FR_APMATERNO,
PARENTESCO = fr.PARENTESCO,
COLONIA = fr.COLONIA,
CALLE = fr.CALLE,
FR_SEXO = fr.FR_SEXO,
NUMERO_EXT = fr.NUMERO_EXT,
NUMERO_INT = fr.NUMERO_INT
});
}
List < PPL_ALIAS > alias = new List < PPL_ALIAS > ();
foreach(PPL_ALIAS a in registro.PPL_ALIAS) {
alias.Add(new PPL_ALIAS {
NOMBRE_ALIAS = a.NOMBRE_ALIAS,
APPATERNO_ALIAS = a.APPATERNO_ALIAS,
APMATERNO_ALIAS = a.APMATERNO_ALIAS
});
}
List < PPL_APODO > apodos = new List < PPL_APODO > ();
foreach(PPL_APODO a in registro.PPL_APODO) {
apodos.Add(new PPL_APODO {
APODO = a.APODO,
});
}
List < PPL_PANDILLA > pandillas = new List < PPL_PANDILLA > ();
foreach(PPL_PANDILLA p in registro.PPL_PANDILLA) {
pandillas.Add(new PPL_PANDILLA {
NOTAS = p.NOTAS,
CAT_PANDILLA = p.CAT_PANDILLA,
});
}
List < PPL_RELACIONES_PERSONALES > relaciones = new List < PPL_RELACIONES_PERSONALES > ();
foreach(PPL_RELACIONES_PERSONALES rp in registro.PPL_RELACIONES_PERSONALES) {
relaciones.Add(new PPL_RELACIONES_PERSONALES {
RP_NOMBRE = rp.RP_NOMBRE,
RP_APMATERNO = rp.RP_APMATERNO,
RP_APPATERNO = rp.RP_APPATERNO,
PARENTESCO = rp.PARENTESCO,
NOTAS = rp.NOTAS
});
}
List < PPL_PERTENENCIAS > pertenencias = new List < PPL_PERTENENCIAS > ();
foreach(PPL_PERTENENCIAS p in registro.PPL_PERTENENCIAS) {
pertenencias.Add(new PPL_PERTENENCIAS {
PERTENENCIA = p.PERTENENCIA,
DESCRIPCION = p.DESCRIPCION
});
}
PPL_REGISTRO r = new PPL_REGISTRO {
AUTORIDAD_DISPOSICION = registro.AUTORIDAD_DISPOSICION,
AUTORIDAD_INTERNADO = registro.AUTORIDAD_INTERNADO,
CLASIFICACION = registro.CLASIFICACION,
EDIFICIO = registro.EDIFICIO,
ESTATUS_ADMINISTRATIVOS = registro.ESTATUS_ADMINISTRATIVOS,
FECHA_INGRESO = registro.FECHA_INGRESO,
FECHA_REGISTRO = registro.FECHA_REGISTRO,
NUM_OFICIO = registro.NUM_OFICIO,
TIPO_INGRESO = registro.TIPO_INGRESO,
TIPO_SEGURIDAD = registro.TIPO_SEGURIDAD,
UBICACION = registro.UBICACION,
DELITO = registro.DELITO,
PPL_DATOS_GENERALES = registro.PPL_DATOS_GENERALES,
PPL_PERTENENCIAS = pertenencias,
PPL_ALIAS = alias,
PPL_APODO = apodos,
PPL_FAMILIAR_RESPONSABLE = familiares,
PPL_RELACIONES_PERSONALES = relaciones,
PPL_MEDIA_FILIACION = registro.PPL_MEDIA_FILIACION,
PPL_PANDILLA = pandillas
};
using(var bl = new BLRegistroPPL()) {
bl.CrearRegistroPPL(r);
}
As you can see I use the entity registro as base to create a new PPL_REGISTRO entity. I take all the values of registro and pass them to a new Entity, and with the relationships I iterate over them and create a new list with complete new objects, and assign them to the new PPL_REGISTRO entity I'm creating.
After that I want to save the new entity in the database using the CrearRegistroPPL method to save it:
public void CrearRegistroPPL(PPL_REGISTRO registroPPL) {
context.PPL_REGISTRO.Add(registroPPL);
context.SaveChanges();
}
But at the moment of save it, the app throw me the next error:
System.InvalidOperationException: 'An entity object cannot be referenced by multiple instances of IEntityChangeTracker.'
What this can be?