Delete Entity

Posts   
 
    
coderdude
User
Posts: 10
Joined: 24-Apr-2006
# Posted on: 24-Apr-2006 16:08:21   

I am using self-servicing and would like to delete an entity with a primary key that is an identity field. I would like to do this directly with one hit to the database (i.e. not select then delete). The docs show this:

Method 2: use a new entity object to directly delete an entity from the database. Just set the PK fields to a value. SelfServicing, deleting the customer "FISSA" from Northwind (doesn't violate any FK constraints) // C# CustomerEntity customer = new CustomerEntity(); customer.CustomerID = "FISSA"; customer.Delete();

When I do this I get the following error message:

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

I assume it is read-only because it is an identity field. So what is the correct way to accomplish this with only one DB hit?

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 24-Apr-2006 16:29:48   

From Generated code - Using the entity classes, SelfServicing

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

coderdude
User
Posts: 10
Joined: 24-Apr-2006
# Posted on: 24-Apr-2006 17:27:13   

Worked perfectly! Thanks!