Entity Framework Core add unique constraint code-first

On EF core you cannot create Indexes using data annotations.But you can do it using the Fluent API. Like this inside your {Db}Context.cs: protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>() .HasIndex(u => u.Email) .IsUnique(); } …or if you’re using the overload with the buildAction: protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<User>(entity => { entity.HasIndex(e => … 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”)));

How to unapply a migration in ASP.NET Core with EF Core

Use: CLI > dotnet ef database update <previous-migration-name> Package Manager Console PM> Update-Database <previous-migration-name> Example: PM> Update-Database MyInitialMigration Then try to remove last migration. Removing migration without database update doesn’t work because you applied changes to database. If using PMC, Try: PM> update-database 0 This will wipe the database and allow you to remove the … Read more