Click here to Skip to main content
15,665,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

I am using VSCode and I am struggling to migrate my database with ef as I have already installed the necessary dotnet packages.My problem is that every time i am trying to do "
sudo dotnet ef migrations add InitialCreate
" it will not complete the process as it says I have a null connectionString parameter(
System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
).I double checked my db string connection which is the following in my appsettings.json:
"ConnectionStrings ": {
  "DataContext": "host=localhost;port=3306;user id=****;password=*******;database=*****;"
In the Startup.cs class I have the following:
public void ConfigureServices(IServiceCollection services)

           var connectionString = Configuration.GetConnectionString("DataContext");

      services.AddDbContext<DataContext>(options => options.UseMySql(connectionString));


Now I don't understand why it's not working since the name is correct along with the syntax in the appsettings.json.I am aware that MySql.Data is not the best solution for migration so instead i am using pomelo.This is my project.csproj file:

    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="3.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />

Any help would be appreciated!

What I have tried:

I have tried to change the code in Startup.cs as follows:
services.AddDbContext<DataContext>(options => options.UseMySql(Configuration.GetConnectionString("DataContext")))
Except this,I don't know what else i could try since the connection is correct and the same code worked before...
Updated 1-Dec-21 4:37am

1 solution

Ok,now i know why it didn't work.One of the packages i installed in the previous project was not in the new one:

Apparently without this one, the migration will fail.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900