[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)
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
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?
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?
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.
If you find the post has answered your issue, then please mark post as 'answered'.
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.
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
4 Points
69 Posts
EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 08, 2021 02:48 PM|Gatwick|LINK
Here's my code
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?
All-Star
53711 Points
24031 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 08, 2021 02:54 PM|mgebhard|LINK
I'm guessing you are missing an entry in the OnModelCreating method.
Contributor
4973 Points
4263 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 08, 2021 03:15 PM|DA924|LINK
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)
Member
4 Points
69 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 08, 2021 03:52 PM|Gatwick|LINK
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
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?
All-Star
53711 Points
24031 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 08, 2021 04:17 PM|mgebhard|LINK
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?
Contributor
4973 Points
4263 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 08, 2021 05:00 PM|DA924|LINK
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.
Member
4 Points
69 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 08, 2021 08:14 PM|Gatwick|LINK
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
it worked w/o any exceptions. I wonder why the first way (with .Entity calls) doesn't work.
Contributor
3550 Points
934 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 11, 2021 07:32 AM|l.laxmikant|LINK
Contributor
3040 Points
863 Posts
Re: EF Core and "The entity type 'EntityEntry<XXXXX>' was not found"
Apr 12, 2021 02:42 AM|YihuiSun|LINK
Hi Gatwick,
The parameter received by the DbContext.Update method should be of entity type.
Incorrect:
email.WasSent = true; var emailAsEntiy = ctx.Entry(email); ctx.Update(emailAsEntiy);
Correct:
Here are some links that can help you.
Best Regards,
YihuiSun