Just don’t use Entity Framework in this case. Just use a stored procedure (how to depends on the version/approach you use with EF, you might will have to extend your DbContext
or add a mapping from the entity model).
If you’re using SQL Server, then in your store procedure, do use the MERGE
command that efficiently does exactly what you need: insert if it doesn’t exist, or update if it does. Everything in a single, efficient SQL query.
Related Contents:
- Code First: Independent associations vs. Foreign key associations?
- Entity Framework: “Store update, insert, or delete statement affected an unexpected number of rows (0).” [closed]
- EF Core Second level ThenInclude missworks
- How do I delete multiple rows in Entity Framework (without foreach)
- Entity Framework 6 Code first Default value
- Entity Framework – Why explicitly set entity state to modified?
- ORM Entities vs. Domain Entities under Entity Framework 6.0
- Improving bulk insert performance in Entity framework [duplicate]
- Disable lazy loading by default in Entity Framework 4
- Batch update/delete EF5
- EntityFramework code-first custom connection string and migrations
- EF Code First “Invalid column name ‘Discriminator'” but no inheritance
- How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?
- How do I detach objects in Entity Framework Code First?
- ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)
- No executable found matching command “dotnet-ef”
- Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?
- How to select top N rows for each group in a Entity Framework GroupBy with EF 3.1
- How to ensure proxies are created when using the repository pattern with entity framework?
- mapping private property entity framework code first [duplicate]
- Debug code-first Entity Framework migration codes
- Asp.Net Web API Error: The ‘ObjectContent`1’ type failed to serialize the response body for content type ‘application/xml; charset=utf-8’
- Cascading deletes with Entity Framework – Related entities deleted by EF
- EF Query With Conditional Include
- Entity framework core update many to many
- Order navigation properties when using Include and/or Select methods with EF 4.1 Code-First?
- Entity framework code-first null foreign key
- EF can’t infer return schema from Stored Procedure selecting from a #temp table
- Using EF Core ThenInclude() on Junction tables
- Entity Framework 4 / POCO – Where to start? [closed]
- How can I disable migration in Entity Framework 6.0
- How do I singularize my tables in EF Code First?
- What is the syntax for self referencing foreign keys in EF Code First?
- Entity Framework: Ignore Columns
- Entity Framework CTP 4 – Code First Custom Database Initializer
- When should I call SaveChanges() when creating 1000’s of Entity Framework objects? (like during an import) [duplicate]
- Entity Framework: Re-finding objects recently added to context
- Why is my Entity Framework Code First proxy collection null and why can’t I set it?
- Entity Framework 4.1+ many-to-many relationships change tracking
- Querying objects after AddObject before SaveChanges?
- Entity Framework Code First : Setting up One-To-One foreign key association using Annotations
- EF Core 2: How to apply HasQueryFilter for all entities
- How to delete an object by id with entity framework
- what is difference between a Model and an Entity
- LINQ to Entities for subtracting 2 dates
- Mocking or faking DbEntityEntry or creating a new DbEntityEntry
- Error 6002: The table/view does not have a primary key defined
- Map two different entities to the same table?
- How do I use Entity Framework in Code First Drop-Create mode?
- check constraint entity framework