How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?

This is a great question. First of all, here are a couple of workarounds that come to mind (they all require changes to the query): First workaround This one maybe a bit obvious and unfortunately not generally applicable: If the selection of items you would need to pass over to Enumerable.Contains already exists in a … Read more

Map two different entities to the same table?

You can’t map two regular entities into same table. You have several choices: Use table splitting. Use custom query with projection to non entity type (as @Aducci proposed) Use QueryView Use database view or directly DefiningQuery Table splitting Table splitting allows you to map a table into two entities in 1:1 relation. First entity will … Read more

“Context cannot be used while the model is being created” exception with ASP.NET Identity

The problem was that we were NOT using the factory pattern that MS recommends. You can use Factory implementation to get an instance of UserManager from the OWIN context. … This is a recommended way of getting an instance of UserManager per request for the application. As a result, “the same context instance is accessed … Read more

MySQL – Entity : The value for column ‘IsPrimaryKey’ in table ‘TableDetails’ is DBNull

Entity Framework (version 6.1.3) and MySQL Server (>= 5.7.6) One way to resolve the issue is, 1. Open Services (services.msc) and restart MySQL57 service. 2. Execute the following commands in MySQL. use <<database name>>; set global optimizer_switch=”derived_merge=OFF”; 3. Update the .edmx. It’s a late reply. But hope it will help somebody. Thanks.

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

As mentioned by @anon you can’t attach model once you loaded the entity with the same key. The changes must be applied to attached entity. Instead of this: db.Entry(model).State = EntityState.Modified; use this: db.Entry(v).CurrentValues.SetValues(model);

How to do an “in” query in entity framework?

Try this: var orderKeys = new int[] { 1, 12, 306, 284, 50047}; var orders = (from order in context.Orders where orderKeys.Contains(order.Key); select order).ToList(); Assert.AreEqual(orderKeys.Count, orders.Count); Edit: I have found some workarounds for this issue – please see WHERE IN clause?: The Entity Framework does not currently support collection-valued parameters (‘statusesToFind’ in your example). To … Read more

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

Using any context.XyzAsync() method is only useful if you either await the called method or return control to a calling thread that’s doesn’t have context in its scope. A DbContext instance isn’t thread-safe: you should never ever use it in parallel threads. Which means, just for sure, never use it in multiple threads anyway, even … Read more