Well, I have the following model structure: I have one class - DatabaseEntitywhich is basically
public class DatabaseEntity
{
public int Id { get; set; }
}
therefore, every object, such as a product, category, etc., inherits DatabaseEntityand has a property Id. I also have a typical repository class EntityFrameworkwith a method InsertOrUpdate:
private readonly DbContext _database;
public void InsertOrUpdate<TObject>(TObject entity) where TObject : DatabaseEntity
{
if(entity.Id == default(int))
{
DbSet<TObject>().Add(entity);
}
else
{
_database.Entry(entity).State = EntityState.Modified;
}
_database.SaveChanges();
}
Then I boot from eBay through the list of eBay api categories that I have to add to the database. Mainly category:
public class EbayCategory : DatabaseEntity
{
public string Name { get; set; }
}
But the problem is that when I load the categories that I load, and their identifier properties, which, of course, already have values. And when I try to save them in a database, for example:
public void UpdateCategories(IEnumerable<EbayCategory> newCategories)
{
foreach (var newCategory in newCategories)
{
_repository.InsertOrUpdate(newCategory);
}
}
... , entity.Id != default(int), , , , :
System.Data.Entity.Infrastructure.DbUpdateConcurencyException
"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."
... , - , . InsertOrUpdate, (EbayCategories) (Id) , / , EbayCategory.Id ?