You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Database First模式下,如何通过Entity Framework从MySQL Workbench数据库自动生成模型至Models文件夹?

Hey there! Welcome to Stack Overflow—super excited to help with your first question 😊. Let's break down exactly what you need to do to generate your MySQL database models into your project's Models folder using Entity Framework's Database First approach.

Required NuGet Packages

First, you'll need to install a couple of packages to get EF working with MySQL. You can do this via the NuGet Package Manager in Visual Studio or using the terminal:

  • MySql.EntityFrameworkCore: The official Entity Framework Core provider for MySQL
  • Microsoft.EntityFrameworkCore.Tools: This adds the scaffolding commands we'll use to generate the models

Generating Models via Terminal Commands

Once the packages are installed, you can use either the Package Manager Console (in Visual Studio) or the .NET CLI (any terminal) to scaffold the models.

Option 1: Package Manager Console (Visual Studio)

Open the Package Manager Console (under Tools > NuGet Package Manager > Package Manager Console) and run this command, replacing placeholders with your MySQL credentials:

Scaffold-DbContext "server=localhost;database=lakeydivolta;user=YOUR_MYSQL_USERNAME;password=YOUR_MYSQL_PASSWORD" MySql.EntityFrameworkCore -OutputDir Models -Force
  • -OutputDir Models: Tells EF to put all generated model files in your project's Models folder
  • -Force: Overwrites any existing models in the folder (useful if you update your database schema later)

Option 2: .NET CLI (Terminal)

If you prefer using the regular terminal, navigate to your project's root directory (where the .csproj file lives) and run:

dotnet ef dbcontext scaffold "server=localhost;database=lakeydivolta;user=YOUR_MYSQL_USERNAME;password=YOUR_MYSQL_PASSWORD" MySql.EntityFrameworkCore --output-dir Models --force

Quick Tips to Avoid Issues

  • Make sure your MySQL server is running before executing the command—otherwise, EF won't be able to connect to your database.
  • Double-check your connection string: If your MySQL server isn't on localhost, update the server value to your actual server address.
  • Ensure the NuGet package versions match your .NET version (e.g., if you're using .NET 7, install the latest compatible version of MySql.EntityFrameworkCore).

Your original database schema for reference:

create database lakeydivolta;
use lakeydivolta;
create table users(
 Username varchar(767) primary key not null,
 FirstName text,
 LastName text,
 Dob datetime,
 Adress text,
 Email text,
 PhoneNumber text,
 HashedPassword varbinary(4000),
 PasswordSalt varbinary(4000)
);
create table videogames(
 Id int primary key auto_increment,
 Title text,
 Description text,
 Platform text,
 Poster text, -- verrà inserito l'url della locandina di gioco
 Price double,
 ReleaseDate datetime,
 CDKey text
);
create table bought(
 primary key(UserUsername,VideoGameId),
 UserUsername varchar(767),
 VideogameId int,
 foreign key(UserUsername) references users(Username) on delete cascade on update cascade,
 foreign key(VideogameId) references videogames(Id) on delete cascade on update cascade
);

内容的提问来源于stack exchange,提问作者Lambdat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 02:17:30