Friday, May 25, 2012

ADO.NET Entity Data Model – Unexpected Number of Rows Affected

I use the Entity Data Model in almost all of my projects now. Major time saver usually, but today I received an error that I hadn’t seen before:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

What gives? I am adding an object to a really simple collection using this code:

using (HCEntities context = new HCEntities())
{
 Alert a = new Alert()
 {
  Description = "test",
  AgentGUID = "aaaa",
  Timestamp = DateTime.Now
 };
 context.AddToAlerts(a);
 context.SaveChanges();
}

The Entity Framework is attempting to insert into this table:


CREATE TABLE [dbo].[Alerts]
(
 [ID]           INT IDENTITY(1,1)  NOT NULL,
 [AgentGUID]    VARCHAR (100)      NOT NULL,
 [Timestamp]    DATETIME          NOT NULL,
 [Description]  VARCHAR(500)      NULL
 CONSTRAINT [PK_Alerts] PRIMARY KEY 
 (
  [ID],
  [AgentGUID], 
  [Timestamp]
 )

 CONSTRAINT [FK_Alerts_Agent] FOREIGN KEY ([AgentGUID]) 
 REFERENCES [dbo].[Agent] ([AgentGUID]) 
 ON DELETE CASCADE ON UPDATE NO ACTION
)

Solution

It turns out that the Entity Framework doesn’t like IDENTITY columns participating in a composite primary key. Given that the identity column is unique by definition, a composite key makes no sense in this scenario anyway.

I updated the primary key definition and the problem was fixed. The exception thrown by the framework is fairly cryptic though.

No comments: