Entity Framework Database First .Net CoreAsk Questions

 Posted on 09/13/2019

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?

Share On: facebook gplus twitter
profile
Asked by Grayson Omans on 09/13/2019 Score: 60 points
Add Comment:

Comments

2 Answers

1 Corrected Answers
Aproved Answers
0
Profile
Answered by hardik chaudhary on 9/28/2019 3:42:44 PM Score: 553 points

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

Make sure the "Microsoft.VisualStudio.Web.CodeGeneration.Design" package is installed:

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

  2. Run Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design

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

Comments

Add Comment:
Aproved Answers
1
Profile
Answered by Praful Chauhan on 9/13/2019 9:22:52 AM Score: 116 points

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

Comments

Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
Password
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.