efcore icon indicating copy to clipboard operation
efcore copied to clipboard

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

Open codingonHP opened this issue 9 years ago • 16 comments

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?

codingonHP avatar Mar 18 '17 05:03 codingonHP

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 avatar Mar 18 '17 06:03 ErikEJ

@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 avatar Mar 18 '17 08:03 codingonHP

@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...

ErikEJ avatar Mar 18 '17 11:03 ErikEJ

@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 avatar Mar 20 '17 18:03 ajcvickers

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

ErikEJ avatar Mar 20 '17 18:03 ErikEJ

@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 avatar Mar 20 '17 18:03 lajones

@lajones Sorry, yes!

ErikEJ avatar Mar 20 '17 18:03 ErikEJ

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

ajcvickers avatar Mar 20 '17 18:03 ajcvickers

@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?)

lajones avatar Mar 20 '17 19:03 lajones

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)

ErikEJ avatar Mar 20 '17 20:03 ErikEJ

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.

lajones avatar Apr 03 '17 23:04 lajones

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 avatar Oct 20 '17 09:10 ntgnst

@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.

ajcvickers avatar Oct 20 '17 17:10 ajcvickers

@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?

dazbradbury avatar Dec 05 '19 21:12 dazbradbury

like this:

zhangxuan1107 avatar Feb 25 '20 02:02 zhangxuan1107

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

zhangxuan1107 avatar Feb 25 '20 02:02 zhangxuan1107