The field '' is read-only and can't be changed

Posts   
 
    
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 23-Feb-2010 12:50:02   

Hello,

I've read the following in the documentation:

Option 2 is more efficient in that it just starts an update, without first reading the data from the database. The following code performs the same update as the previous example code illustrating option 1. Even though the PK field is changed, it is not updated, because it is not previously fetched from the database.

C#VB.NET // [C#] CustomerEntity customer = new CustomerEntity(); customer.CustomerID="CHOPS"; customer.IsNew=false; customer.Phone = "(605)555-4321"; customer.Save();

We have to set the primary key field, so the Update method will only update a single entity, the "CHOPS" entity. Next, we have to mark the new, empty entity object as not being new, so Save() will call the Update method, instead of the Insert method. This is done by setting the flag IsNew to false. Next is the altering of a field, in this case "Phone", and the call of Save(). This will not load the entity back in memory, but because Save() is called, it will be marked out of sync, and the next time you'll access a property of this entity's object, it will be refetched from the persistent storage. Doing updates this way can be very efficient and you can use very complex update constructs when you apply an Expression to the field(s) to update. See for more information about Expression objects for fields Field expressions and aggregates.

Now I try this code for myself:


public void UpdateLocation(int locationId, string name, string reception, string email)
        {
            //Make a new instance of LocationEntity. Since IsNew is false and the primary key (LocationId) is set an update query will be executed.
            LocationEntity locationEntity = new LocationEntity()
            {
                IsNew = false,
                LocationId = locationId,
                Name = name,
                Reception = reception,
                Email = email
            };

            //Update the row into sql server.
            locationEntity.Save();
        }

I did this before if the GUID was a primary key, but now I use an int with identity seed and I get the following exception:

The field 'LocationId' is read-only and can't be changed

What am I doing wrong?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 23-Feb-2010 21:14:28   

public void UpdateLocation(int locationId, string name, string reception, string email)
        {
            //Make a new instance of LocationEntity. Since IsNew is false and the primary key (LocationId) is set an update query will be executed.
            LocationEntity locationEntity = new LocationEntity(locationId)
            {
                IsNew = false,
                Name = name,
                Reception = reception,
                Email = email
            };

            //Update the row into sql server.
            locationEntity.Save();
        }

The LocationId field is read-only because it is a generate field - it has to be set in the entity constructor, as shown above.

Matt

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 24-Feb-2010 07:57:46   

MTrinder wrote:


public void UpdateLocation(int locationId, string name, string reception, string email)
        {
            //Make a new instance of LocationEntity. Since IsNew is false and the primary key (LocationId) is set an update query will be executed.
            LocationEntity locationEntity = new LocationEntity(locationId)
            {
                IsNew = false,
                Name = name,
                Reception = reception,
                Email = email
            };

            //Update the row into sql server.
            locationEntity.Save();
        }

The LocationId field is read-only because it is a generate field - it has to be set in the entity constructor, as shown above.

Matt

That's how I currently solved it, but in this situation I first need to fetch the data and than update it meaning an select and update query will be executed. I only want to execute an update query like in my sample above (and as described in option 2 in the manual). I already did this several times when I had a GUID (with a sequentialid) as primary key and it worked. This should also work for intergers right?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 24-Feb-2010 10:17:34   

This is caused by the identity primary key used in SelfServicing scenario. Please check this note on the manual (just below the section you have quoted):

If you want to set an identity primary key column, you'll notice you can't do that because it is marked as read-only. Use the method entityObject.Fields[fieldindex or fieldname].ForcedCurrentValueWrite(value). See the reference manual for details about this method (EntityField.ForcedCurrentValueWrite).