Am currently trying out my hand in .net core and EF core.
I have the following code to update the data
protected void Update(T entity, bool saveChanges = true)
{
_context.Set<T>().Attach(entity);
_context.Entry(entity).State = EntityState.Modified;
if (saveChanges) _context.SaveChanges();
}
The class I am updating
public partial class Blog
{
public Blog()
{
Post = new HashSet<Post>();
}
public int BlogId { get; set; }
public string Url { get; set; }
public virtual ICollection<Post> Post { get; set; }
}
When I try to update any entry the first time, it is successful. The second time I update the same entry, I get the below error:
System.InvalidOperationException: 'The instance of entity type 'Blog' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values for new entities, ensure they do not collide with existing entities or temporary values generated for other new entities. When attaching existing entities, ensure that only one entity instance with a given key value is attached to the context.'
The same occurs for each entry, successful for the first time and fails the second time.
Additional Information: I get all the data using the below code:
protected IEnumerable<T> GetAll()
{
return _context.Set<T>().AsNoTracking().AsEnumerable();
}
Display the above as a table in the view. DTO's are being used to communicate between the data and web layer.
The context is registered in startup as below
services.AddDbContext<BloggingContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
My question is why there is an error during the second update and how to resolve it. Thanks.
services.AddScoped<BlogRepository, BlogRepository>();this is how BlogRepository is used in startup - gvk