Colbs
Colbs

Reputation: 597

Entity Framework Core models

I have built a ASP.NET Core MVC application that connects to MySQL. I wanted to enhance the solution by adding Entity Framework because it automatically generates entities and models for you. But reading about this new version (EF Core), it looks like you have to create the models in code then run a script to create the database. That seems backwards, what is the benefit if you don't want to create the database at runtime?

I thought Entity Framework was the opposite, you could create the database first, then add connect it in Visual Studio through a nice UI and it would generate everything for you. Why is it reversed now? I would rather maintain the database in MySQL, then fetch / sync the changes in Visual Studio.

Upvotes: 2

Views: 961

Answers (2)

Colbs
Colbs

Reputation: 597

What worked for me was Pomelo.EntityFrameworkCore.MySql

The run this command with a connection string:

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

Use

 -o Models 

to generate classes in the Models directory

Upvotes: 0

Camilo Terevinto
Camilo Terevinto

Reputation: 32072

It is true that Entity Framework Core (for now, at least) only supports the Code First approach, but that only means that the option to generate classes through EDMX files and T4 (normally using the visual designer) is gone.

You have two options for Code First, just as you do in Entity Framework 6:

  1. From scratch. Create your classes as you want, you design your database through the classes.
  2. By reverse-engineering the database to create the classes. You can see here how to do this: Getting Started with EF Core on ASP.NET Core with an Existing Database.

Both options will leave you tied only to your POCOs and you can decide whether you want to use migrations or not.

Upvotes: 1

Related Questions