Reputation: 119
So I'm doing this project in Entity Framework Core with npgsql as my provider. There's a Case class, which is the main class for my solution. With relation one-to-many there's also CaseCustomFieldValue class. Here's the code for them:
public class Case
{
public int CaseId { get; set; }
public List<CaseCustomFieldValue> CaseCustomFieldValues { get; set; }
}
public class CaseCustomFieldValue
{
public int CaseCustomFieldValueId { get; set; }
public string Value { get; set; }
public int CaseId { get; set; }
public virtual Case Case { get; set; }
}
Idea is that CaseCustomFieldValue will be posted/modified/deleted together with the case, but when I'm trying to update value of CaseCustomFieldValue it doesn't change. So I added this into my PUT method:
_context.Entry(@case.CaseCustomFieldValues).State = EntityState.Modified;
In result above line fails with error:
InvalidOperationException: The entity type 'List<CaseCustomFieldValue>' was not found. Ensure that the entity type has been added to the model.
I think I did everything needed in DbContext class:
modelBuilder.Entity<Case>()
.HasMany(a => a.CaseCustomFieldValues);
modelBuilder.Entity<Case>().ToTable("Cases");
modelBuilder.Entity<CaseCustomFieldValue().ToTable("CaseCustomFieldValues");
public DbSet<Case> Cases { get; set; }
public DbSet<CaseCustomFieldValue> CaseCustomFieldValues { get; set; }
I dropped the database and recreated it from database update, but it didn't help.
Upvotes: 4
Views: 6287
Reputation: 11
just check if the object you are passing is a DTO or Entity, people often get confused when they are adding an entity, but end up passing DTO.
Upvotes: 1
Reputation: 429
I had this issue using the incorrect method to start tracking the model. I was using AddAsync instead of AddRangeAsync for my list of models.
Upvotes: 3