What app.UseMigrationsEndPoint does in .NET Core Web Application Startup class

This app.UseMigrationsEndPoint() is actually a very handy tool in development. As we develop, we add entities to the db context, or modify ones we have. We run dotnet ef migrations add <NameOfMigration> as usual, and that would generate migration files. I personally would typically run dotnet ef database update when the migrations add command succeeds. … Read more

EF Core migrations in Docker container

In my opinion, it is your first point (Database.Migrate() due startup) that meets mostly our use case. So for me, it`s currently the preferred way to do that. We have some additional constellations in the starting up process: Docker container locally only (for dev environment and testing for sure) Own startup project which executes the … Read more

Update ContextModelSnapshot EF Core

You can execute the command Add-migration temporary to create a new empty migration. Then, run Remove-Migration temporary (or their dotnet-cli counterparts) In recent editions of EF Core (3+), just use: Remove-Migration (will revert the last migration) It will create model snapshot from scratch even if the migration has already been deleted. This approach works perfectly … Read more

How to get primary key value with Entity Framework Core

I also faced with similar problem and found the following solution // Entity Framework Core public virtual int GetKey<T>(T entity) { var keyName = Context.Model.FindEntityType(typeof (T)).FindPrimaryKey().Properties .Select(x => x.Name).Single(); return (int)entity.GetType().GetProperty(keyName).GetValue(entity, null); }

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: Log queries for a single db context instance

Call DbContextOptionsBuilder.UseLoggerFactory(loggerFactory) method to log all SQL output of a particular context instance. You could inject a logger factory in the context’s constructor. Here is a usage example: //this context writes SQL to any logs and to ReSharper test output window using (var context = new TestContext(_loggerFactory)) { var customers = context.Customer.ToList(); } //this context … Read more

The entity type ‘Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin’ requires a key to be defined

Basically the keys of Identity tables are mapped in OnModelCreating method of IdentityDbContext and if this method is not called, you will end up getting the error that you got. This method is not called if you derive from IdentityDbContext and provide your own definition of OnModelCreating as you did in your code. With this … Read more