Creating ASP.NET Core project with React and Authentication
When creating a new ASP.NET Core project with React and built-in authentication (IdentityServer), you can use the following command (see here for more information):
dotnet new react -o my-new-app -au Individual
This will create a new project with ASP.NET as an API backend, standard create-react-app (CRA) as the front end, and IdentityServer for authentication and authorization. However, the default database is SQLite. If you try to switch to SQL Server, you’ll run into the following error:
Entity Framework Core: Column ‘Id’ in table ‘Users’ is of a type that is invalid for use as a key column in an index
This is because the initial EF migration files were generated with SQLite data types. You will need to delete and regenerate the migration files for SQL Server.
- Uninstall
Microsoft.EntityFrameworkCore.Sqlite
- Install
Microsoft.EntityFrameworkCore.SqlServer
- Update the connection string
- In
Startup.cs
, updateConfigureServices()
to use SQL Serverservices.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection")));
- Remove existing migration files (because they’re meant for SQLite)
/Data/Migrations/00000000000000_CreateIdentitySchema.cs /Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs /Data/Migrations/ApplicationDbContextModelSnapshot.cs
- Regenerate the migration files for SQL Server
dotnet ef migrations add CreateIdentitySchema --output-dir "Data/Migrations"
- Run EF Migration to generate the initial tables