views:

76

answers:

4

Hi, I'm using ADO.NET Entity framework for my project and I am new at this techonology. There are 2 associated tables, one is "personel" other is "departmant". When I tried to update personel's departmant I am getting this error:

[System.InvalidOperationException] = {"'DEPARTMANID' property is piece of object's key information and can not be changed. "}

Here is my code below for update;

int DepartmantId = 1;
int PersonelID = 2; 
    try
            {
                using (FirebirdEntityz fe = new FirebirdEntityz())
                {

                var query = (from c in fe.PERSONEL.Include("DEPARTMANT") where c.PERSONELID == PersonelID select c).First();
                query.NAME = NAME;
                query.SURNAME = SURNAME;
                query.DEPARTMANT.DEPARTMANTID = DepartmantId;

                int result = fe.SaveChanges();
                if (result > 0)
                {
                    MessageBox.Show(result + " record updated");
                }

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.InnerException.ToString());
        }
A: 

I'm not familiar with that framework at all, but if I had to guess, I'd say that you're trying to the change the primary key in the table and your framework won't let you.

Darthg8r
A: 

Key properties can't be changed once an entity has been created and saved, and from that error it seems that DepartmentId is defined as part of Personnel's primary key.

If you need to change your primary key, that's usually a good indication that your "key" isn't really a key.

You should consider adding a column to your Personnel schema for a unique key that doesn't change, something like PersonnelId (int) that is an identity column (assuming you are using SQL Server).

Good luck!

thinkzig
+1  A: 

Indeed it's a problem with changing a primary key of an entity. Just, I believe it's throwing exception because you are trying to change the primary key of DEPARTMENT entity, not PERSONNEL.

Main thing that you want to accomplish here is to change the value of the foreign key,right? If so you can do it in two ways:

1.Without round trips to dbs:

Replace

query.DEPARTMANT.DEPARTMANTID = DepartmantId

with

query.DEPARTMENTReference.EntityKey = new EntityKey("YourDataContext.DEPARTMENT", "DEPARTMENTId", value);

2.By making round trips to the database to get the new DEPARTMENT you want to set to the PERSONNEL entity:

using (FirebirdEntityz fe = new FirebirdEntityz())
        {

 var query = (from c in fe.PERSONEL.Include("DEPARTMANT") where c.PERSONELID == PersonelID select c).First();
            query.NAME = NAME;
            query.SURNAME = SURNAME;
            query.DEPARTMANT.DEPARTMANTID = DepartmantId;
 //query.DEPARTMANT.DEPARTMANTID = DepartmantId
            var newDepartment = (from d in fe.DEPARTMENT
                                    where d.DEPATMENTID==DepartmentId
                                    select d).First();

            query.DEPATMENT = newDepatment;

            int result = fe.SaveChanges();
            if (result > 0)
            {
                MessageBox.Show(result + " record updated");
            }

        }
Misha N.
A: 

What you ar doing in this line

query.DEPARTMANT.DEPARTMANTID = DepartmantId;

is:

  • You're opening your personel entity
  • Then you're opening the relatet department entity
  • Then you're changing the primary key of your department entity.

What I guess what you really wanted to do is:

  • Opening personel entity
  • Changing the foreign key of the the personel entity.

To do so, use this code:

using (FirebirdEntityz fe = new FirebirdEntityz())
{
    var department = (from d in fe.DEPARTMENT where d.ID == DepartmentID select d).FirstOrDefault();
    if(department==null)return;
    var query = (from c in fe.PERSONEL where c.PERSONELID == PersonelID select c).FirstOrDefault();
    if(query==null)return;
    query.NAME = NAME;
    query.SURNAME = SURNAME;
    query.DEPARTMANT = department;

    int result = fe.SaveChanges();
}
Chrigl