I am carrying out a small test and if it works, I embed the code in a large project in which I work, which is with MVC . My problem is that I try to insert 4 records using the for loop, but when I try to do it, it throws me the following error
The property 'idPermiso' is part of the object's key information and cannot be modified
this is my code
static void Main(string[] args)
{
using (var BD = new DATA.TESTEntities1())
{
var Unum = new DATA.Table_6();
for (int i = 1; i <= 4; i++)
{
Unum.idPermiso = i;
Unum.idUsuario = 55;
Unum.idConvenio = 878;
Unum.estaActivo = true;
BD.Table_6.Add(Unum);
BD.SaveChanges();
}
}
}
He inserts the first record successfully but fails when it loops through the for and my permissionid is set to 2
My table contains 4 columns and I would like my INSERT to stay like this
idPermiso idUsuario idConvenio estaActivo
1 55 878 1
2 55 878 1
3 55 878 1
4 55 878 1
The structure of my table is as follows, the column isActive is [ bit] , that is, true and false .
The rest are of type [bigint] and PRIMARY KEY and do not allow NULL
What could be wrong with my code that throws me this error?
I appreciate the help you can give me
The error occurs because after executing
SaveChanges()
the object stored inUnum
it becomes part of the context. That can be fixed by creating a new objectTable_6
on each iteration.