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

Command line connection string for EF core database update

In EF Core 5.0, you will pass the connection string in the command line like this, dotnet ef database update –connection “connection string” Reference: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#new-command-line-parameters-for-namespaces-and-connection-strings

EF Core One to One or Zero Relationship

Here .HasForeignKey<Person>(a => a.PersonID) you are telling EF that Person.PersonID will be a FK (foreign key) to Address, i.e. Person is dependent and is referencing the principal Address. It should be other way around: .HasForeignKey<Address>(a => a.PersonID) This way, Person (the principal) will have 0..1 Address, and Address (the dependent) will have 1 Person (because … Read more

The LINQ expression could not be translated and will be evaluated locally

You have to be aware of the difference between IEnumerable and Iqueryable. An IEnumerable object represents a sequence of objects. It holds everything to enumerate over this sequence: you can ask for the first element of the sequence, and once you’ve got an element you can ask for the next one, as long as there … Read more

Specifying ON DELETE NO ACTION in Entity Framework 7? [duplicate]

After digging around on GitHub, and working with a very patient guy from MS there, the current solution is to add this to the DbContext protected override void OnModelCreating(ModelBuilder modelbuilder) { foreach (var relationship in modelbuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) { relationship.DeleteBehavior = DeleteBehavior.Restrict; } base.OnModelCreating(modelbuilder); }

EF Core – Table ‘*.__EFMigrationsHistory’ doesn’t exist

Turning Mark G’s comment into an answer. Once the __EFMigrationsHistory table has been created, the rest of the update should run. CREATE TABLE `__EFMigrationsHistory` ( `MigrationId` nvarchar(150) NOT NULL, `ProductVersion` nvarchar(32) NOT NULL, PRIMARY KEY (`MigrationId`) ); Alternatively, generate the script of your migration(s) and apply to the database manually using this command in Package … Read more

Entity Framework | Sequence contains more than one matching element

You have net6.0 target framework which is still not released while you have installed EF6 which is a previous iteration Entity Framework (mainly used with legacy .NET Framework projects) and you also have EF Core (a modern iteration of it) but older version – 5.0 (which you are actually using for your context, see the … Read more

The DbContext of type cannot be pooled because it does not have a single public constructor accepting a single parameter of type DbContextOptions

When using DbContext Pooling, your own state (e.g. private fields) in your derived DbContext class will be preserved. Which means the lifetime of your services is now singleton. That’s why you shouldn’t have other injected services here. But it’s possible to query the required services this way: First we should use the UseInternalServiceProvider method on … Read more