Efcore: Question: How to scaffold only few tables in DB first approach in EF Core.

Created on 18 Mar 2017  路  16Comments  路  Source: dotnet/efcore

I am trying to do database first development in EF Core 1.1. I am following https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db to do so. What I am guessing from the command
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
is that it will generate DbContext containing all the tables in the database. My current database is having over 1000 tables and I do not need all of them for the current project. Is there a way to target only specific tables that I need during the scaffolding process?
Also, do I need to scaffold tables at all if I only need to execute Stored Procedures? Can anyone guide me on this?

closed-question

Most helpful comment

@codingtonHP - the docs on Scaffold-DbContext are here. Specifically you can include just a specific set of tables or all the tables from a specific set of schemas. See the -Tables and -Schemas flags in the documentation.

@ErikEJ - I think you meant the command-line tool only allows inclusions not exclusions.

All 16 comments

For the selecting tables issue, you can use my (unofficial) VS Tool to generate the code, and it has a UI for selecting tables: https://twitter.com/ErikEJ/status/834405829889638402

@ErikEJ Thanks :)
Any thoughts on

Also, do I need to scaffold tables at all if I only need to execute Stored Procedures? Can anyone guide me on this?

@codingonHP Yes: why use EF Core if you only need to execute stored procedures?
If the stored procedures return results, you need some classes in your DbContext to map them to, and if they match some of your tables, then scaffolding makes sense...

@lajones Can you point @codingonHP to the documentation to do this without Erik's tool? (If we need to improve the docs, then let's make sure there is an issue for that.)

@ajcvickers The main issue is that the current tools /command line only allows you to specify "exclusions", not "inclusions"

@codingtonHP - the docs on Scaffold-DbContext are here. Specifically you can include just a specific set of tables or all the tables from a specific set of schemas. See the -Tables and -Schemas flags in the documentation.

@ErikEJ - I think you meant the command-line tool only allows inclusions not exclusions.

@lajones Sorry, yes!

@divega Is this documentation okay, or should we make it more prominent?

@divega @ajcvickers I would like to put the whole section under Tools & Extensions as well as / instead of under Miscellaneous. At least that's where I first looked for it. (That said, the things currently under Tools & Extensions look very different - so maybe we need to rename one or more of the sections?)

How about moving the command line reference a level up, so it is the same level in TOC as API Reference (it is a large part of the EF Core surface)

I've opened https://github.com/aspnet/EntityFramework.Docs/issues/375 to track the docs issue. Am closing this as, @codingonHP, I think we have answered your question.

Well , how can i costomize modelBuilder on model creating for Auto Increment properties and how add ValueGenerateOnAdd() method for all models best practise.
Thanks

@ntgnst When reverse engineering an existing database, the scaffolding tool should configure properties to be store-generated auto-incrementing if they are setup that way in the database. (However, note that for numeric keys, the default behavior in EF is to use store-generated keys, so this is not written out to OnModelCreating because it happens by convention anyway.)

If you are finding a case where the model generated does not match the database, then please file a new issue with full details and a code listing or project that will let us reproduce what you are seeing.

@codingtonHP - the docs on Scaffold-DbContext are here. Specifically you can include just a specific set of tables or all the tables from a specific set of schemas. See the -Tables and -Schemas flags in the documentation.

@ErikEJ - I think you meant the command-line tool only allows inclusions not exclusions.

Will there ever be an exclusions flag? I can see arguments for both - just wondering if the decision is fixed at this point?

like this:

Scaffold-DbContext "Database=netchargedb_new;Data Source=127.0.0.1;User Id=root;Password=1234;CharSet=utf8;port=3306" MySql.Data.EntityFrameworkCore -tables t_menu,t_menu_power -OutputDir Models -f

Was this page helpful?
0 / 5 - 0 ratings