Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse engineer model from database #830

Closed
rowanmiller opened this issue Oct 8, 2014 · 40 comments
Closed

Reverse engineer model from database #830

rowanmiller opened this issue Oct 8, 2014 · 40 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@rowanmiller
Copy link
Contributor

Looks like we didn't have a work item tracking this. We need tooling to reverse engineer a model from a database. For the initial cut this should include the functionality from EF6.x (ability to select tables etc.).

This does not include the ability to incrementally update the model based on changes to the database, #831 tracks that.

@lajones
Copy link
Contributor

lajones commented Jul 1, 2015

Moved comment from #2465.

From @ErikEJ: Is this a good time to start investigating using Reverse Engineering with SQLCE, or is it too early?

From @divega: @ErikEJ I personally believe it is a good time for you to get started with it. If you hit issues or have any feedback we can make adjustments.

From ErikEJ: @divega Sounds great, is there some docs/design specs? Or a drawing? 😄

I have a few questions:
1: Does this info still apply: https://stackoverflow.com/questions/29300777/is-there-an-entity-framework-7-database-first-poco-generator?
2: If so, how does the command know which provider to use?
3: Is the rev-eng package simply installed as needed? (EntityFramework.SqlServer.Design)
4: Will I "simply" implement the equivalent of EntityFramework.SqlServer.Design ?

@lajones
Copy link
Contributor

lajones commented Jul 1, 2015

@ErikEJ @divega

  1. The commands listed in https://stackoverflow.com/questions/29300777/is-there-an-entity-framework-7-database-first-poco-generator are still accurate.
  2. That's the problem - at the moment it's hard-coded to use EntityFramework.SqlServer.Design. @rowanmiller are you ok if I add back in the --provider flag to pass in the name of the RevEng provider assembly? Also if the argument is not provided do you want to a) error or b) default to the existing value?
  3. At the moment you must reference EntityFramework.SqlServer.Design in your project.json and hence it is installed when package restore is run on the project. For your new provider your new package should be referenced in project.json instead.
  4. Yes. Hopefully simply :-). You must have a class that implements IDatabaseMetadataModelProvider. I'll write this up but in the meantime details below.

A) On IDatabaseMetadataModelProvider, GenerateMetadataModel(string connectionString) generates an IModel containing EntityTypes. DbContextTemplate() and EntityTypeTemplate() return the relevant templates as strings (you get to look them up however you want - however note #2418 where we'll be providing a way for customers to override the default templates).

B) First the DbContextTemplate template is executed being passed an instance of DbContextGeneratorModel. On DbContextGeneratorModel you'll find a property MetadataModel - which is the model you generated in step A) above.

C) Then we loop over all EntityTypes in the MetadataModel and on each one execute the EntityTypeTemplate passing in an EntityTypeGeneratorModel. Note that on EntityTypeGeneratorModel you'll find a property EntityType which is the EntityType being processed (and which can refer to its parent - the metadata model - if needed).

D) Each Model passed in also has a Helper property - which is an instance of DbContextCodeGeneratorHelper or EntityTypeCodeGeneratorHelper respectively. These contain helpful methods for ordering the entities, setting up configuration for the DbContext based on the model etc. Feel free to inherit from these classes and override methods or provide new ones as you see fit (but bear in mind #2465).

@rowanmiller
Copy link
Contributor Author

I think we need to quickly discuss how we want the "provider invariant name" to work in EF7. My vote is that we just use the assembly/package name, but we should discuss this.

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 1, 2015

@lajones Thanks, that should be enough to get me going, will also await your "unblocking" of the hardcoded provider name.
Re 3: - This is my understanding of the current workflow, can you sanity check?

a: As a developer, I want to reverse engineeer my existing SQL CE database to use it with EF7.
b: In my project.json (if I have one) or using Package Manager Console, I add the
EntityFramework7.SqlServerCompact40.Design package in my Data Access project
b: To use the Reverse-Engineer command from PMC I must also add the EntityFramework7.Commands package
d: I can now run the "Reverse-Engineer" command, specifying a connection string and the name of the design package (or dll) : EntityFramework7.SqlServerCompact40.Design
e: Now some code will be generated in my Data Access Projects that models my existing database file schema

Is that just about correct?

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 10, 2015

@lajones @divega @rowanmiller Rev Eng now works for the SQLCE provider! Thanks for your timely support 😄

@rowanmiller
Copy link
Contributor Author

Sweet 🎉

@divega
Copy link
Contributor

divega commented Jul 10, 2015

@ErikEJ not sure if I have said this before but... you rock! 😄

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 10, 2015

@divega @lajones Thanks to your excellent groundwork!

@lajones
Copy link
Contributor

lajones commented Jul 10, 2015

@ErikEJ You're v welcome. Thanks for all your hard work!

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 11, 2015

@lajones And the E2E tests revealed a couple of small bugs in my SQL CE scripting engine: https://sqlcetoolbox.codeplex.com/SourceControl/changeset/96873 - thanks!

@lajones
Copy link
Contributor

lajones commented Jul 11, 2015

@ErikEJ - Glad to help!

@lajones
Copy link
Contributor

lajones commented Jul 30, 2015

Closing this over-arching issue for the whole of RevEng. Now we'll track each individual bug/feature as it comes up. Raised issue #2751 to track ability to select tables.

@lajones lajones closed this as completed Jul 30, 2015
@lajones lajones modified the milestones: 7.0.0-beta7, 7.0.0 Jul 30, 2015
@claysmith
Copy link

How do you specify the output path for ef scaffold reverse engineering?

@lajones
Copy link
Contributor

lajones commented Sep 4, 2015

A lot has changed since the above comments. If you do dnx ef dbcontext scaffold --help it will give you the options. Specifically there's a -o option on the command line to specify a sub-directory of whatever project you are in.

@claysmith
Copy link

I am running CLR-x64-1.0.0-beta7-15532 and -o is not an option

@alfberga
Copy link

alfberga commented Sep 4, 2015

And i'm running CLR-x64-1.0.0-beta7-15540 and -o is not an option too.
And no DataAnnotation in reverse engineering generated models

@ErikEJ
Copy link
Contributor

ErikEJ commented Sep 4, 2015

You need to run a beta 8 daily build for that feature to work. And you need to customize the templates in order to force the tooling to use DataAnnotations (I am working on a blog post)

@lajones
Copy link
Contributor

lajones commented Sep 4, 2015

@claysmith - that's correct - the -o option was checked in with commit 5b19fbb. Before that you could only output to the project path. You are of course free to move the files after they've been output.
@ErikEJ - don't work too hard on that blog post. Things have been churning a lot in this area (which is why I haven't written any docs about it). #2947 changes it so we no longer allow customizable templates.

@ErikEJ
Copy link
Contributor

ErikEJ commented Sep 4, 2015

@lajones thanks for the heads up! How do you switch between attribute and fluent then?

@lajones
Copy link
Contributor

lajones commented Sep 4, 2015

@ErikEJ Current plan is to have a command-line switch - see PR #3013

@alfberga
Copy link

Sorry for an old question: when do you think to develop the reverse engineering of Views?

@rowanmiller
Copy link
Contributor Author

@alfberga that will happen after the initial RTM of EF7.

@lajones is there any way to track reverse engineer into thinking a view is a table at the moment?

@lajones
Copy link
Contributor

lajones commented Nov 30, 2015

You mean "trick" reverse engineering? No I'm afraid not. Each provider retrieves the corresponding metadata in their own *DatabaseModelFactory code.

On the other hand it's not likely to be complicated to add it. We have an existing issue for this: #1679. It's been on the Backlog for a while - perhaps it's time to revisit it?

@roji
Copy link
Member

roji commented Dec 4, 2015

FYI everyone, I've just published the Npgsql reverse engineering provider. I've confirmed that the EF7 database-first tutorial works just fine with it.

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 4, 2015

Congrats!

@lajones
Copy link
Contributor

lajones commented Dec 4, 2015

Yep congrats! Really cool!

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-beta7, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

8 participants