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



I've Been Vaccinated Wristband



work form home items,essentials,home office furnitures,laptops

afforable web hosting plans
Reliable Security & High Speed web hosting  plans
Microsoft Surface Book 3
InterServer Web Hosting and VPS
free .com domain and best cheap hosting plans
Premium Subscription Offer
  • see more..