0

I use ASP.NET Core 6 and use Entity Framework with an existing database.

Since my database already exists, I need to do reverse engineer to generate the code based on the existing database. I have seen some tutorials which show how I can do it. But what I see is, I am only able to get the tables but not other objects like, stored procedure, views, functions or triggers.

I have two questions:

  1. When working with the database first approach where the database already exists in EF Core, when reverse engineering do I have to generate code for the other objects like I mentioned above, stored procedures, views and other?

  2. Is it optional or must I have them?

  3. If they are required how can I generate them in C# code?

3
  • EF does only generate mappings for those objects it needs to access data. Other stored procedures can be called, but are not part of the OR mapping. Commented May 25, 2022 at 5:36
  • SQL Server Data Tools (SSDT) lets you reverse engineer a complete database, with all objects, into a Visual Studio project. You can then edit within the project and publish your updates to the database. But it has nothing to do with Entity Framework. Commented May 25, 2022 at 10:45
  • So what happen in the situation if I make a change in table then I can run the migration script which would update underline database. I thought I can generate a script from the EF/VS project that can run on any db for update purpose production db. Now if I don't have Stored proc or views in C# code then update migration script would miss those objects and won't be good to apply to prod db. Therefore what could be the best solution to have that done?.Maybe I can stick to create a manual db script or create a seperate db project in VS to generate the change db script for production dbs Commented May 26, 2022 at 7:51

1 Answer 1

1

You can only generate models from existing database using this command -

DOTNET CLI:

dotnet ef DBContext scaffold "Server=.;Database=db_name;User Id=user_id;Password=password" Microsoft.EntityFrameworkCore.SqlServer -o  Models

PACKAGE MANAGER CONSOLE:

Scaffold-DbContext "Server=.;Database=db_name;Trusted_Connection=True;Id=user_id;Password=password" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

This command will reverse engineer db tables to models.

Sign up to request clarification or add additional context in comments.

1 Comment

OKay I understood that I can migrate only tables into C# code by using reverse engineering technique. What if I need to add a stored procedure or function. Would I have to define that in to C# code? I seen some article where they are defining the sp in the project. I want to understand the situation when we are defining them in the code. for example see this article c-sharpcorner.com/UploadFile/ff2f08/…. Another thing is what if I want to produce the release script to apply on production how would I do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.