Entity Framework Database First .Net Core
Asked Answered
J

3

11

I have a .Net Standard 2.0 class library project and I want to add Entity Framework to it. I have added the Entity Framework Core package to the project but no Entity Framework Templates appear when I try to add a new item to the project. I have looked on the web and all I can seem to find is instructions for Code First!

How do I get the database first functionality back?

Juncaceous answered 31/1, 2018 at 22:8 Comment(0)
U
15

EF Core does not, and will never, support the EDMX-based Database First workflow with the designer. EF Core stores all object-to-database mapping in Attributes and Fluent API mapping in your source code.

In EF 6 the term "Code First" meant two very different things. One is a code-first modeling workflow where your database was generated from your .NET classes. The other meaning of "Code First" was just that the mapping metadata was embedded in your source code (Attributes/Fluent API) rather than in an EDMX file. EF 6 supported two different database-first workflows. Database-first with the EDMX, and the workflow officially called "Code First From an Existing Database", but which could have been called "Database-First with Code-Based Mapping".

In EF Core, your code will always have the mapping, and so in that sense it's "code first". But you can still do a database-first design workflow, and write entities and mapping code that match your existing database.

And you can use the scaffold-dbcontext in the Package Manager Console, or dotnet ef dbcontext scaffold in the CLI command to generate entity classes and mapping metadata from an existing database. See Getting Started with EF Core on ASP.NET Core with an Existing Database

Updraft answered 31/1, 2018 at 22:34 Comment(7)
Thank you this clarifies things. I tried to overcome this (Core EDMX) conflict by adding my EDMX based ORM in a separate project (Have done this Pre-core) as well. However, my Core Web Project is not accepting it as a dependency " The dependency "EF_Project" could not be resolved." Is still a lost cause o to conflicting Frameworks?Quirinus
If you're using EDMX, then you're using EF6, not EF Core. And so your project must use .NET Framework, not .NET Core.Updraft
I am using VS2015 and my main (or StartUp) Project is a ASP.CORE Web Application using .NET Framework (rather then .NET Core).Quirinus
Thanks for the answer. It clears a few things up. However Forcing people to use the CLI is ludicrous and seems a major step backwards.Juncaceous
"EF Core does not, and will never, support the EDMX" OK, I get that. But now what is the approach for a database that exists, and will have changes/additions to it as time goes by? What is the path where code-first is not an option due to corporate concerns? What's the process for quick prototyping and making quick table changes? It seems like a lot of functionality has been dropped. It seems primitive and unfinished to me.Ursas
Just Reverse Engineer the model whenever it changes in the database. Keep any customizations in partial classes so they don't get overwritten. learn.microsoft.com/en-us/ef/core/managing-schemas/…Updraft
Finally a succinct and laconic explanation of Entity Framework theory and intent. Its often confusing building out a new EF project from scratch without any background on this stuff, and the reading required to understand it is substantial and can be overwhelming...Lubumbashi
E
1

In ".net core", you can not use EF from the menu and the "add a new item" button.

Make sure the "Microsoft.EntityFrameworkCore.Tools" package is installed:

  1. Goto Tools –> NuGet Package Manager –> Package Manager Console

  2. Run Install-Package Microsoft.EntityFrameworkCore.Tools

Then follow these steps:

  1. Create a connection string by the sample code. (SQLSERVER,DATABASE,USERNAME,PASS)

    Data Source=SQLSERVER;Initial Catalog=DATABASE;persist security info=True;user id=USERNAME;password=PASS

  2. Edit the sample code and place the code above in the quotation. (CONECTIONSTRING,FOLDERNAME)

    Scaffold-DbContext "CONECTIONSTRING" Microsoft.EntityFrameworkCore.SqlServer -OutputDir FOLDERNAME

  3. For example, this is the result of steps 1 and 2.

    Scaffold-DbContext "Data Source=localhost;Initial Catalog=myDb;persist security info=True;user id=sa;password=0000" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/EF

  4. Run the code above through the Tools –> NuGet Package Manager –> Package Manager Console

More details on the Getting Started with EF Core on ASP.NET Core with an Existing Database

Esmeralda answered 25/7, 2018 at 22:59 Comment(3)
What a pitch! ."NET Core- setting functionality back 20 years just in case your server doesn't have Windows!"Pamphlet
@Pamphlet this is exactly my thoughts too. Why after all these years of using a gui for interacting with EF am I now forced to remember long CLI commands? It really is ludicrous. Not only that but adding EF to one project required a journey down the rabbit hole as it kept complaining that dependencies were in conflict and so I had to install each dependency individually. 30-40 dependencies later I managed to get it installed! I feel future projects will be going back to .net 4Juncaceous
Probably due to the simplicity of the dotNet Core structure and its optimization, it is not possible to perform the operation as a GUI. But we should see what Microsoft will do in Visual Studio 2019 and dotNet Core 3 for this process.Esmeralda
L
0

You can also reverse engineering your database schema, which according to the docs it means:

It is the process of scaffolding entity type classes and a DbContext class based on a database schema. It can be performed using the Scaffold-DbContext command of the EF Core Package Manager Console (PMC) tools or the dotnet ef dbcontext scaffold command of the .NET Command-line Interface (CLI) tools.

Then, if you already have an existing database you only need to perform a command with your connection string and the actual provider's NuGet package name. For example, for a postgresql database you will need the next command:

dotnet-ef dbcontext scaffold "Host=localhost;Username=my_user_name;Password=my_pswd;Database=my_db" Npgsql.EntityFrameworkCore.PostgreSQL
Logy answered 22/4, 2022 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.