aspnet / MusicStore

[Archived] MusicStore test application that uses ASP.NET/EF Core. Project moved to https://github.com/aspnet/AspNetCore
1.3k stars 878 forks source link

AddOrUpdateAsync broken and misleading #536

Closed staff0rd closed 7 years ago

staff0rd commented 9 years ago

AddOrUpdateAsync in SampleData.cs implies that you can run it even if the data exists already. However, it would seem that EntityFramework does not support the following call and will throw an exception if the data exists:

db.Entry(item).State = existingData.Any(g => propertyToMatch(g).Equals(propertyToMatch(item)))
    ? EntityState.Modified
    : EntityState.Added;

Exception

The instance of entity type 'MusicStore.Models.Genre' cannot be tracked because another instance of this type with the same key is already being tracked. For new entities consider using an IIdentityGenerator to generate unique key values.

AddOrUpdateAsync however is only ever called if the database is being created, due to EnsureCreatedAsync here.