I try to add entity through the navigation property of collection, but the following message comes up:
"Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions."
The models are:
SuggestionGroupDb:
public class SuggestionGroupDb
{
public Guid Id { get; set; }
[Required]
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual TeguUserDb User { get; set; }
[Required(AllowEmptyStrings=false, ErrorMessage = "Required")]
[StringLength(30, MinimumLength = 1, ErrorMessage = "Invalid")]
public string Name { get; set; }
public int OrderNo { get; set; }
public virtual ICollection<SuggestionItemDb> Items { get; set; }
}
SuggestionItemDb:
public class SuggestionItemDb
{
public Guid Id { get; set; }
[Required]
public Guid SuggestionGroupId { get; set; }
[ForeignKey("SuggestionGroupId")]
public virtual SuggestionGroupDb SuggestionGroup { get; set; }
[Required(AllowEmptyStrings=false, ErrorMessage = "Required")]
[StringLength(30, MinimumLength = 1, ErrorMessage = "Invalid")]
public string Name { get; set; }
public int OrderNo { get; set; }
}
SuggestionGroup Repository Update function (simplified):
public async Task<SuggestionGroupRepositoryResult> UpdateAsync(string userid, SuggestionGroupDb suggestiongroup)
{
// Step 01 - Get the Entity
var dbSuggestionGroup = await GetAsync(userid, suggestiongroup.Id, suggestiongroup.Name);
// Step 02 - Update the items (just add one now)
foreach (var item in suggestiongroup.Items)
{
var sidb = new SuggestionItemDb() {Id = item.Id, Name = item.Name, OrderNo = item.OrderNo, SuggestionGroupId = item.SuggestionGroupId};
dbSuggestionGroup .Items.Add(sidb);
}
// Step 03 - Update the changes
try
{
var updated = context.AccSuggestionGroups.Update(dbSuggestionGroup);
await context.SaveChangesAsync();
return new SuggestionGroupRepositoryResult("Valid") /*{SuggestionGroup = updated.Entity}*/;
}
catch (Exception e)
{
context.Reset();
return new SuggestionGroupRepositoryResult("Failed", e.Message);
}
}
The problem is that SaveChanges throws and exception with the given message. Is it possible to update the SuggestionItems through the SuggestionGroup?
I am using EF Core 3.0 preview 6.