Update Database using Entity Framework having primary key on multiple Columns

asp.net-mvc asp.net-mvc-4 c# entity-framework linq

Question

Following is the error message I get:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

My model consists of few columns, among them two columns which make up the primary key clustered:

[Key]
[Column(Order = 0)]
public string SourceName { get; set; }
[Key]
[Column(Order = 1)]
public string SourceType { get; set; }

Code to update using Entity Framework:

 _Entities.SourceInfoes.Attach(entity);
 _Entities.Entry(entity).State = EntityState.Modified;
 _Entities.SaveChanges();

Can some one help me on how to solve the exception above?

1
4
4/2/2015 12:03:19 AM

Popular Answer

For me, the problem was Unique Index on some of the columns in my database. Violating the index was causing this error to be displayed.

But when I was searching for the solution I found there can be other reasons for this error also , like make sure key have values and are not null , check concurrence setting for the column .

0
10/28/2019 2:35:04 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow