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

Scaffold-DbContext includes "using statement" with model´s namespace #18405

Merged
merged 1 commit into from
Oct 17, 2019
Merged

Scaffold-DbContext includes "using statement" with model´s namespace #18405

merged 1 commit into from
Oct 17, 2019

Conversation

pacoweb
Copy link
Contributor

@pacoweb pacoweb commented Oct 16, 2019

Summary of changes

  • Add a new parameter to the WriteCode method of the ICSharpDbContextGenerator interface to pass the namespace of the models
  • CSharpDbContextGenerator generates a using statement in the DbContext class if the models are in another namespace
  • Add two test on CSharpDbContextGeneratorTest

Fixes #17839

Summary of changes

- Add a new parameter to the WriteCode method of the ICSharpDbContextGenerator interface to pass the namespace of the models
- CSharpDbContextGenerator generates a using statement in the DbContext class if the models are in another namespace
- Add two test on CSharpDbContextGeneratorTest

Fixes #17839
@dnfclas
Copy link

dnfclas commented Oct 16, 2019

CLA assistant check
All CLA requirements met.

@bricelam bricelam self-assigned this Oct 16, 2019
@@ -62,9 +62,10 @@ public class CSharpDbContextGenerator : ICSharpDbContextGenerator
/// </summary>
public virtual string WriteCode(
IModel model,
string @namespace,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should keep old API for provider compatibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless this goes to master.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, is ICSharpDbContextGenerator .Internal too? I feel like it should be

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too late to make anything internal for a while now...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's Internal. This isn't breaking.

@bricelam
Copy link
Contributor

I wonder if we should consider #3988 when fixing this. The implication there is that the entity types could be in multiple namespaces. Would putting the root model namespace in model be a better approach?

@ajcvickers
Copy link
Member

@bricelam Are you suggesting that we accept multiple model namespaces in the overload being added here? If so, I agree.

@smitpatel
Copy link
Member

@ajcvickers - Model base namespace would be same but if there are different schemas then there would be many different subnamespaces, all of which should be added to using. So we need a better mechanism to tell DbContextGenerator that these are the namespaces where entityTypes are generated. Probably shelf this for next release. We have already punted it for 3.1

@bricelam
Copy link
Contributor

bricelam commented Oct 16, 2019

What I had in mind: This method gets the IModel already, so maybe we add a step before this that populates it with annotations for the namespace of things. This would just foreach through the entity types and add the usings it needs.

@ajcvickers
Copy link
Member

@bricelam Entity type names should be namespace-qualified anyway, so do we need other information? Or is this not the case when we get the model for reverse engineering?

@bricelam
Copy link
Contributor

Not the case when we get the model for reverse engineering currently, but it could be. I think that's the right change.

@ajcvickers
Copy link
Member

@bricelam Would that be breaking to, for example, @ErikEJ?

@bricelam bricelam added this to the 5.0.0 milestone Oct 16, 2019
@bricelam
Copy link
Contributor

Good point. Yes, it would break code generators not expecting it (e.g. every Handlebars-based template)

@bricelam bricelam removed this from the 5.0.0 milestone Oct 16, 2019
@bricelam
Copy link
Contributor

Given that all this is Internal, I'm OK taking this in 3.1. @ajcvickers thoughts?


if (finalContextNamespace != modelNamespace)
{
_sb.AppendLine(String.Concat("using ", modelNamespace, ";"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use...

_sb.Append("using ").Append(modelNamespace).AppendLine(";");

@ajcvickers
Copy link
Member

@bricelam Okay for 3.1--#17839 is a bug after all.

@bricelam bricelam merged commit 4d17b42 into dotnet:release/3.1 Oct 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scaffold-DbContext with -ContextDir flag doesn't add namespace for models
5 participants