Scaffold EF Core from Database Project
Asked Answered
H

1

6

How can I scaffold EF Core directly from a Visual Studio SQL Server Database Project?

Solutions such as the following are preferred:

scaffold-dbcontext -connection "provider=ssdtproject, name=myprojectname.sqlproj"
scaffold-dbcontext -ddl "ssdtprojectoutput.sql"
scaffold-dbcontext -ssdtschema "ssdtproject.dacpac"
maintained-third-party-tool myprojectname.sqlproj -EfModelGenerationParameters

That's the whole question. What follows is my situation in more detail, so that you may be able to offer alternate solutions:

Although MS acknowledges EF Core is still not production-ready, it's also now 3-4 years since EF 6 progress ceased, and EF Core is the only LINQ code-similar path forward with NETCore compatibility. Thus begins the saga titled "So you're going to be using EF Core."

This part is opinionated, but to me (based on 25+ years of enterprise software design and development experience) Code-First is an absolute non-starter. It's fine for small week-one application concepts, but there's no reasonable pattern/process/practice that I can see to integrate constraints, views, etc. Without views designed-in, real business apps end up with devs repeating logic fundamentals in LINQ expressions all over the place, littering the code with static fields to support LINQ-to-SQL queries, confusing micro-combinatory patterns using LinqKit, etc. Without constraints we end up with ten times the defensive code requirements to handle runtime errors, rapidly blossoming unit and integration tests, and demo failures become the norm. Either our object-oriented experts need to become SQL experts or the converse, and we drastically increase the difficulty of finding and properly-compensating engineers. All of these issues I pointed out in a detailed conversation four years ago with Rowan Miller (who recently left the EF team, which doesn't bode well for near-term solutions).

Model-First (the visual .edmx designer in prior EF versions) is obviously off the table, since the MS solution to this was to claim Code-First really IS Model-First, and wash their hands of it. Consequently a truly neutral, let's call it "Contract-First" for clarity, approach doesn't exist in EF Core.

So, that rant (sorry, frustrated) brings me to Database-First, and thus Scaffold-DbContext. Our DB Schema is currently a revision-controlled Visual Studio SQL Server Database Project. Aside some known issues with this, it also seems ridiculous to have to take our DB schema (currently our single-point-of-truth), rebuild a live database from it, and then back-generate code from the live database, all as part of our build process just to verify database type alignment. I'd like instead to be able to simply detect changes and regenerate my DbContext and related Entities directly from the Database Project.

SSDT Database Projects seem to make Database-like objects available in many of the UIs where normally database connections are required. That makes me think it may be a short walk to use the database schema as a source for existing tools. For example, use a metadata provider in a connection string, make a simple modification to the EF Core code, etc.

SQL Sharpener "generate[s] at design-time using SQL files as the source-of-truth (such as those found in an SSDT project", and was recommended as a solution to this problem for previous versions of EF, but it does not support EF Core.

SQLite and SQL Server Compact Toolbox just added support for generating EF Models directly from .DACPAC, but it appears to depend on the EntityFramework Reverse POCO Code First Generator for that functionality, which prominently lists "Support EF Core" on its TODO List. The primary contributor of this project confirms that incompatibility.

Help?

Hopefully answered 2/9, 2017 at 22:14 Comment(6)
I think you can still use the SQL Sharpener-based solution. All you have to do is modify the T4 template that is given in the examples to generate files that are EF Core compatible.Aerostation
Also, couldn't disagree more regarding Code First. I'd be interested in reading/listening to that discussion with Rowan Miller, if you still have any links or references.Aerostation
Would love to give you the transcript, but the blog page, titled "ef7 what does code first only really mean", was recently deleted. And it's common for people to disagree on this topic. You'll find that the people who publicly support code first are typically OO-focused, while the people who decry the loss of DB-first are typically RDBMS-focused. Personally I've been both and I welcome the debate with you offline.Hopefully
Regarding modifying Sharpener: creating a replacement T4 template that supports EF Core would require committing the team to learning t4, supporting a proprietary tool through EF changes, resolving the issue of Sharpener outputting a single code file (not really suitable for revision-controlled environments) web.archive.org/web/20160409042105/http://www.olegsych.com:80/… , handling an increasing load of edge cases in the t4 template as our database requirements grow, etc. etc. It wouldn't be my first choice.Hopefully
Please comment on #1186 to let the team know you really want this.Joule
I've build a database first generator that supports both EF6 and EFCore context generating, maybe it would be a good start for your project? we can discuss.Joycejoycelin
O
1

I was struggling with the same until I ran into the sublime EF Core Power Tools extension for Visual Studio. Its reverse engineering tool sounds just like what you need.

https://marketplace.visualstudio.com/items?itemName=ErikEJ.EFCorePowerTools

Occupational answered 29/9, 2021 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.