locked
EF Core and "The entity type 'EntityEntry<XXXXX>' was not found" RRS feed

  • Question

  • User1993186009 posted

    Here's my code 

    [Table("mailboxOut")]
    public class DbMailOut
    {
        [Key]
        [Column("Id")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long Id { get; set; }
    
    ...
    
        [Column("WasSent")]
        public bool WasSent { get; set; } = false;
    }
    
     
    
    public class MailboxSvcDbContext : DbContext
    {
        public DbSet<DbMail> Emails { get; set; }
        public DbSet<DbMailOut> EmailsOut { get; set; }
    
    .....
    
    var ctx = new MailboxSvcDbContext (); var emails = ctx.EmailsOut.Where(x => !x.WasSent).ToList(); foreach (Management.DataLayer.Models.Mailbox.DbMailOut email in emails) { ... email.WasSent = true; var emailAsEntiy = ctx.Entry(email); ctx.Update(emailAsEntiy);

    Right at the last line I get the following exception:

    System.InvalidOperationException: The entity type 'EntityEntry<DbMailOut>' was not found. Ensure that the entity type has been added to the model.
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetOrCreateEntry(Object entity)
    at Microsoft.EntityFrameworkCore.DbContext.EntryWithoutDetectChanges[TEntity](TEntity entity)
    at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
    at Microsoft.EntityFrameworkCore.DbContext.Update[TEntity](TEntity entity)

    What did I do wrong? 

    Thursday, April 8, 2021 2:48 PM

Answers

  • User1686398519 posted

    Hi Gatwick, 

    Gatwick

    I wonder why the first way (with .Entity calls) doesn't work.

    The parameter received by the DbContext.Update method should be of entity type.

    1. DbContext.Update Method
    2. But the type of emailAsEntiy is an entry for a given entity.
    3. So you get the following error message:
      • The entity type 'EntityEntry<DbMailOut>' was not found

    Incorrect:

    email.WasSent = true;
    var emailAsEntiy = ctx.Entry(email);
    ctx.Update(emailAsEntiy);

    Correct:

    • The type of msg is an entity.
    var msg = ctx.S3Messages.Single( x=> x.Id == id);
    msg.WasRead = true;
    ctx.Update(msg);
    ctx.SaveChanges();

    Here are some links that can help you.

    1. Modifying data via the DbContext
    2. How to update record using Entity Framework Core?

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 12, 2021 2:42 AM

All replies

  • User475983607 posted

    I'm guessing you are missing an entry in the OnModelCreating method.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DbAmilOut>().ToTable("mailboxOut");
    }

    Thursday, April 8, 2021 2:54 PM
  • User1120430333 posted

    Is this for an existing database? If so, why don't you use EF Core for an existing database and scaffold the model?

    Entity Framework Core with Existing Database (entityframeworktutorial.net)

    Thursday, April 8, 2021 3:15 PM
  • User1993186009 posted

    I use this DbContext for already existing database, and it has no custom implementation for OnModelCreating(). And absence of OnModelCreating does not stop it from working with other entities like

    var msg = ctx.S3Messages.Single( x=> x.Id == id);
    msg.WasRead = true;
    ctx.Update(msg);
    ctx.SaveChanges();

    I don't quite understand what's the reason for me of having something extra aside from already defined attributes-based markup in my entities. Could you please explain it for me?

    Thursday, April 8, 2021 3:52 PM
  • User475983607 posted

    I'm not sure what you're asking.  Did you try the suggestion and it worked?  Didn't work?  Are you following Code First patterns?  Maybe you need to scaffold the entity from the DB?   

    Thursday, April 8, 2021 4:17 PM
  • User1120430333 posted

    I use this DbContext for already existing database, and it has no custom implementation for OnModelCreating(). And absence of OnModelCreating does not stop it from working with other entities like

    No, I don't think you have used EF Core's  scaffolding against an existing database that builds the dbcontext and the model classes it derived from the database tables for you.

    You should follow the tutorial that was given to you that will create everything for you. You point to a temp folder or make a temp folder,  do the scaffold pointing to the temp folder, and then you copy the code out the temp folder to the dbcontext you have in your program replacing everything.

    EF scaffolding will do everything for you correctly, and all you have to do is copy the created code to your program.

    It's that simple.

    Thursday, April 8, 2021 5:00 PM
  • User1993186009 posted

    Are you following Code First patterns?

    I am trying to keep DB-specific information as much as it possible in markup attributes. That's whey I don't use OnModelCreating, and in other parts of my application all DB operations work flawlessly w/o OnModelCreating with long listing of data bindings. Meanwhile when I wrote 

    email.WasSent = true;
    ctx.Update(email);

     it worked w/o any exceptions. I wonder why the first way (with .Entity calls) doesn't work.

    Thursday, April 8, 2021 8:14 PM
  • User503812343 posted

    email.WasSent = true;
    ctx.Update(email);

    is it updating data in database?

    Sunday, April 11, 2021 7:32 AM
  • User1686398519 posted

    Hi Gatwick, 

    Gatwick

    I wonder why the first way (with .Entity calls) doesn't work.

    The parameter received by the DbContext.Update method should be of entity type.

    1. DbContext.Update Method
    2. But the type of emailAsEntiy is an entry for a given entity.
    3. So you get the following error message:
      • The entity type 'EntityEntry<DbMailOut>' was not found

    Incorrect:

    email.WasSent = true;
    var emailAsEntiy = ctx.Entry(email);
    ctx.Update(emailAsEntiy);

    Correct:

    • The type of msg is an entity.
    var msg = ctx.S3Messages.Single( x=> x.Id == id);
    msg.WasRead = true;
    ctx.Update(msg);
    ctx.SaveChanges();

    Here are some links that can help you.

    1. Modifying data via the DbContext
    2. How to update record using Entity Framework Core?

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 12, 2021 2:42 AM