Troublesooting asp.net core blazor webassembly dbcontext
Prem Murmu
on
3/26/2021 3:49:30 AM
I was creating asp.net core blazor webassembly pwa app, during changing its dbcontext and connectionstring setting, i faced some errors so i am giving going to reveal all my troubleshooting tips and thier solutions.
when i changed database connectinstring in appsetting.json
From:
"ConnectionStrings": {
"DefaultConnection": "DataSource=app.db"
},
To local DB server as below:
"ConnectionStrings": {
"DefaultConnection": "Server=DESKTOP-QFH04R; Database=mypiblogsDBs; Trusted_Connection=True; MultipleActiveResultSets=true"
},
and tried to update database:
CLI:
dotnet ef database update
Package manger Console:
Update-Database
I gives error as follow:
Connection string keyword 'server' is not supported.
you may get other errors like below
1. Connection string keyword 'database' is not supported.
2. Connection string keyword 'initial catalog' is not supported.
You may get other solutions, but i will describe tips i used here to resoveld.
Actually we create blazor webassembly with authentication identity server it creates default sqllite template support dbcontext.
We get this in Startup class configureservice method like below.
services.AddDbContext< ApplicationDbContext>( options =>
options.UseSqlite (
Configuration.GetConnectionString( "DefaultConnection")));
This configuration gives us error like above. This need to be changed to sql server entityframeworkcore template type as following:
Dbcontext service to registere like below
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
Install related nuget library.
Cli:
1. dotnet add package Microsoft.EntityFrameworkCore --version 5.0.4
2. dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Nuget Manger:
1. Microsoft.EntityFrameworkCore
2. Microsoft.EntityFrameworkCore.SqlServer
After building when you run the project, you may get error like below:
Column 'Id' in table 'AspNetRoles' is of a type that is invalid for use as a key column in an index.
It is telling us to decorate the id properety of aspnetroles entity class, so we just give key attribut to the id property in entity class.
but here in our case AspNetRoles entity class is in identity server entity, it don't recomend to override the property anyhow and decorate with key attribute. Entityframeworkcore have its power to do it bydefault, by just re create migrations and fixes above issue.
So we just delete the existing migrations and create new migration as follow:
CLI:
1. dotnet ef migrations add initial
2. dotnet ef database update
Package manage console:
1. Add-Migration initials
2. Update-Database -versbose
Now you will not get errors and also check identity tables are created in your db.
If you get other errors just comment below.
0 Comments on this post
Comments(0)||Login to Comments
- see more..
Categories
ALLAsp.Net (3)
Sitecore (11)
Issues Resolved (9)
Sitecore 9.3 (2)
International Business (1)
Innovations in Industries (3)
Supply Chain Management (1)
Industrial Engineering (14)
Sitecore 10 (4)
Sharepoint (1)
Azure (4)
Cloud Computing (1)
Online Courses (1)
Online Resources (2)
C# (1)
Blazor WebAssembly (2)
Sitecore form (1)
MS Sql Server (1)
Angular 11 (2)
Automobile Engineering (5)
Sitecore powershell (1)
Devops (2)
Microservice Architecture (1)
Sitecore Interview Questions (2)
Dotnet interview questions (1)
Sitecore useful links (1)
Privacy&Cookies (2)
Solr Search (2)
JavaScript (1)
sitecore xm cloud (3)