Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidOperationException: The entity type <typename> was not found. Ensure that the entity type has been added to the model

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.

like image 847
PRHMN Avatar asked Oct 24 '25 23:10

PRHMN


1 Answers

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.

like image 183
Corey Jensen Avatar answered Oct 26 '25 12:10

Corey Jensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!