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


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));
           services.AddControllersWithViews();

       }

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:
<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <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>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <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>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <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" />
  </ItemGroup>

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...
Posted
Updated 1-Dec-21 4:37am
v2

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:
Microsoft.AspNet.Identity.EntityFramework

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