How do I delete multiple rows in Entity Framework Core? [duplicate]

because I get an error “InvalidOperationException: Collection was modified; enumeration operation may not execute” after the first object. In other words, .Remove removes only one object. This has nothing to do with EF Core, and, yes, .Remove() only removes one object. However, you are attempting to modify a collection that you are iterating through. There … Read more

Entity Framework core – Contains is case sensitive or case insensitive?

It used to be the case for older versions of EF core. Now string.Contains is case sensitive, and for exemple for sqlite it maps to sqlite function `instr()’ ( I don’t know for postgresql). If you want to compare strings in a case-insensitive way, you have DbFunctions to do the jobs. context.Counties.Where(x => EF.Functions.Like(x.Name, $”%{keyword}%”)).ToList(); … Read more

ASP – Core Migrate EF Core SQL DB on Startup

A note from documentation on the call to db.Database.EnsureCreated(): Note that this API does not use migrations to create the database. In addition, the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is … Read more

Entity Framework Core: DbContextOptionsBuilder does not contain a definition for ‘usesqlserver’ and no extension method ‘usesqlserver’

First we install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package: PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer Then, after importing the namespace with using Microsoft.EntityFrameworkCore; we add the database context: services.AddDbContext<AspDbContext>(options => options.UseSqlServer(config.GetConnectionString(“optimumDB”)));

EF Core 2: How to apply HasQueryFilter for all entities

In case you have base class or interface defining the IsActive property, you could use the approach from Filter all queries (trying to achieve soft delete). Otherwise you could iterate entity types, and for each type having bool IsActive property build dynamically filter expression using Expression class methods: foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { var … Read more

How to make EF-Core use a Guid instead of String for its ID/Primary key

You need custom ApplicationUser inherit from IdentityUser<TKey> and custom Role inherit from IdentityRole<TKey> public class ApplicationUser : IdentityUser<Guid> { } public class Role : IdentityRole<Guid> { } Custom context class inherit from IdentityDbContext<ApplicationUser, Role, TKey> and use fluent api for auto generate guid keys. public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid> { protected override void … Read more

tech