J86
J86

Reputation: 15237

EF Core MySQL Database First?

I have a MySQL database sitting in Amazon Cloud (RDS). It is a tiny database with only one table.

I want to use EF Core, Database First. I know that the Pomelo.EntityFrameworkCore.MySql package is popular, but I cannot see any information on how to achieve a scaffolding of the Db Context via that package.

I followed the instructions here whilst replacing MySql.Data.EntityFrameworkCore.Design with Pomelo.EntityFrameworkCore.MySql but when I ran the command below in Package Manager Console:

Scaffold-DbContext "Server=my-db.rds.amazonaws.com;database=TestDb;uid=blah;pwd=blah;" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -f

I just got the error:

The specified framework version '2.0' could not be parsed

What am I missing? Here's how my dummy solution looks

screenshot of solution

Upvotes: 4

Views: 9654

Answers (3)

Gvs Akhil
Gvs Akhil

Reputation: 2581

In 2020 with .net core 3 released a lot of things changed, this is what working as of now :)

Make sure you have all these packages

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

Now dotnet ef is a global tool that needs to be installed, reference

dotnet tool install --global dotnet-ef

Then run this command, reference

dotnet ef dbcontext scaffold "Server=localhost;Database=ef;User=root;Password=123456;TreatTinyAsBoolean=true;" "Pomelo.EntityFrameworkCore.MySql"

For scaffolding in a different path, you can use --output-dir

dotnet ef dbcontext scaffold server=localhost;port=3306;database=palle2patnam;uid=root;password=password" "Pomelo.EntityFrameworkCore.MySql" --output-dir Models

For forcing to get new fields from database u can add --force

dotnet ef dbcontext scaffold "server=localhost;port=3306;database=palle2patnam;uid=root;password=password" "Pomelo.EntityFrameworkCore.MySql" --output-dir Models --force

Upvotes: 4

Dmitry Pavlov
Dmitry Pavlov

Reputation: 28290

You need to have these references in your .csproj. Of course, the versions might vary.

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0-preview2-final" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.1" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
</ItemGroup>

<ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.1.0-preview1-final" />
</ItemGroup>

Note: I had to manually add DotNetCliToolReference to Microsoft.EntityFrameworkCore.Tools.DotNet (not via Manage Nuget Packages view in the Visual Studio)

Upvotes: 3

ZAMEER THAKUR
ZAMEER THAKUR

Reputation: 1

Install the following NuGet packages by selecting either Package Manager Console or Manage NuGet Packages for Solution from the Tools and then NuGet Package Manager menu:

Microsoft.EntityFrameworkCore.Tools version 2.0.1 (for EF Core 2.0)

Pomelo.EntityFrameworkCore.MySql version 2.0.1

After Installing the above run the below command in Package Manager Console:

Scaffold-DbContext "Your connection string here" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -f

Upvotes: -1

Related Questions