-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Generate DbContext Class in a Different Location #10356
Conversation
b8df271
to
6697ebf
Compare
The CI build failed because of a timeout. I ran the build and tests locally and they all passed, although it took more than an hour for them to complete. Is there a more efficient way to run the tests? Update: I figure this is something I'll have to live with. But I'm using |
0df292b
to
f080304
Compare
@bricelam I implemented the update for the C# generator, but I'll probably need to do the same for VB and F#. Can you point me in the right direction? |
Currently there are a couple of failing tests, most likely because I'm doing something untoward. One test, DbContextScaffoldCommand_accepts_separate_context_output_path, fails with The other test, OperationExecutor_ScaffoldContext_generates_separate_context_output_path, fails with a null reference exception on line 106, probably because the Any help diagnosing these would be greatly appreciated! 😄 |
I think the scaffolding code generators are responsible for way too much at the moment. I'm going to play around with the factoring a bit and try to move out the code that writes the files to disk. In Migrations, the flow looks something like this: var scaffoldedMigration = migrationsScaffolder.ScaffoldMigration(); // Calls IMigrationsCodeGenerator
var files = migrationsScaffolder.Save(scaffoldedMigration); This also helps with testing since you don't need to intercept the I/O using something like |
P.S. We should also try and get some of this code out of the |
Excellent - I’m looking forward to the new flow and glad to play a part in it! And it will make testing a lot easier too. :)
|
Waiting on #10459 to merge, then I'll refactor. |
@tonysneed - If you can rebase on latest dev. 😄 |
I should have time for this later this week.
|
test/ef.Tests/CommandsTest.cs
Outdated
var connectionString = new SqlConnectionStringBuilder | ||
{ | ||
DataSource = @"(localdb)\MSSQLLocalDB", | ||
InitialCatalog = "CommandConfiguration", // TODO: Ensure database exists |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't bother with adding end-to-end tests yet. We have #9111 to figure out the best approach for this.
f080304
to
1c3eea8
Compare
@bricelam You can review this, now that I've merged upstream commits and refactored. I've added
Please let me know what you think. |
f1bb47e
to
4ce6c7c
Compare
|
||
var contextPath = Path.Combine(outputDir, scaffoldedModel.ContextFile.Path); | ||
var contextPath = Path.Combine(outputDbContextDir, scaffoldedModel.ContextFile.Path); | ||
File.WriteAllText(contextPath, scaffoldedModel.ContextFile.Code, Encoding.UTF8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bricelam There used to be an IFileService
interface that abstracted the file system so you could mock it. Is there an alternative to that now?
4ce6c7c
to
4977d9f
Compare
Wondering if someone might be able to review this sometime soon? |
Sure, I think I'm done making breaks in this area. 😉 Can you rebase again? |
4977d9f
to
ab23139
Compare
@bricelam I rebased again and resolved conflicts. I think the code it working properly at this point. However, one of my tests (Microsoft.EntityFrameworkCore.Design OperationExecutor_ScaffoldContext_generates_separate_context_output_path) is getting an exception on
It looks like a dependency needs to be registered, but I'm not sure how to do that in the context of my unit test. Would you be able to have a look? |
Changes look good. I suspect we just forgot to add the following to .AddSingleton<IValueConverterSelector, ValueConverterSelector>() |
Is this something I can fix? Or maybe open a separate issue?
|
I can fix it as part of merging this. |
(We know we need #9111 to catch things like this.) |
@bricelam Can you also remove the Or I can re-test after you have registered |
Maybe this feature will make it into the next release? |
Yep. I'll try to get it merged for 2.1.0-preview1. (Working on some 2.0.x patch things at the moment.) |
Thanks! I think this tweak to the tooling will be appreciated by a lot of folks, especially those using repository patterns. /cc @lelong37
|
@tonysneed Forgive me. I'm tweaking this slightly since #8434 will want to put the configuration classes next to the DbContext. There were also a couple of missing things (the corresponding PowerShell changes and a resource). I removed some tests too 🐑 (sheepishly) since we need to figure out the best way to write functionals for RevEng (issue #9111), but I manually verified everything is working end to end. Let me know if you have any concerns with my updates. I also filed #10727 to clarify the semantics of this option. |
d14a4bb
to
a7a1d65
Compare
@bricelam Looks great - thanks! |
Update tests for *nix
80137f3
to
bc1e0a0
Compare
Update tests for *nix
bc1e0a0
to
eecaa01
Compare
- Add outputDbContextDir to ReverseEngineerScaffolder.Save - Add outputDbContextDir to OperationExecutor.ScaffoldContext - Add outputDbContextDir to DbContextScaffoldCommand
eecaa01
to
1e5756a
Compare
Glad to see this merged! 🎉 |
Thanks @tonysneed! |
Address #1836 by generating
DbContext
class to a different location.For my first pass, I refactored
ReverseEngineeringScaffolder.Generate
to accept aoutputDbContextPath
parameter for specifying a different location for theDbContext
class.Next I'll look at how to refactor OperationExecutor.ScaffoldContext.