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

Dbcontext OnConfiguring method generated by scaffold command. #20526

Closed
ansarizafar opened this issue Apr 4, 2020 · 12 comments · Fixed by #20811
Closed

Dbcontext OnConfiguring method generated by scaffold command. #20526

ansarizafar opened this issue Apr 4, 2020 · 12 comments · Fixed by #20811
Assignees
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@ansarizafar
Copy link

I am using following ef command to scaffold postgres db

dotnet ef dbcontext scaffold "connectionString" Npgsql.EntityFrameworkCore.PostgreSQL --schema tribe -o Data -f -c "ApplicationDbContext"

The scaffold command is generating ApplicationDbContext class with OnConfiguring mehod

      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder.UseNpgsql("connectionString");
            }
        }

Is it possible to tell scaffold command to stop generating OnConfiguring method. I don't need this method as I am using

    services.AddDbContext<ApplicationDbContext>(options =>
            options.UseNpgsql(Configuration.GetConnectionString("Postgres")));

in startup,cs file, Is it also possible to avoid following warnings in generated code?

ApplicationDbContext.ApplicationDbContext()
Non-nullable property 'Tribes' is uninitialized. Consider declaring the property as nullable. [tribeserver]csharp(CS8618)
Non-nullable property 'Users' is uninitialized. Consider declaring the property as nullable. [tribeserver]csharp(

```
@ansarizafar
Copy link
Author

I have found a solution for the warning issue but its inconvenient to edit all DbContext and entity classes properties to avoid warnings, every time we scaffold. It would be great If generated code fixed this issue automatically.

        public DbSet<Tribe> Tribes { get; set; } = default!;

@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 4, 2020

Latest daily build of EF Core Power Tools removes OnConfiguring completely (if you choose to not generate a connection string)

@ansarizafar
Copy link
Author

@ErikEJ What about Non-nullable property warnings. Is this issue also fixed in Latest daily build? How can I install Latest daily build of EF Core Power Tools?

@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 4, 2020

I have no idea, you should probably share your project file. For the daily build, look at the readme in the Power Tools repo ( not an official Microsoft project)

@ajcvickers
Copy link
Member

@ansarizafar Are you aware of the name= syntax for connection strings? Is this something you considered?

@ansarizafar
Copy link
Author

@ajcvickers I have used name= syntax but still OnConfiguring method is generated by scaffold command.

Is it there a way to avoid Non-nullable property is uninitialized warnings in generated code?

@ajcvickers
Copy link
Member

@bricelam Is it required that OnConfiguring be used with the name= syntax?

@bricelam
Copy link
Contributor

bricelam commented Apr 7, 2020

Well, we need to get the Name= connection string from somewhere at run time. If you're manually handling it somewhere like Startup.ConfigureServices then we don't need to scaffold OnConfiguring

@ansarizafar
Copy link
Author

ansarizafar commented Apr 8, 2020

@ajcvickers Scaffold is also generating an empty constructor which is causing an error and If I also remove this constructor then the project builds successfully.

   public ApplicationDbContext()
        {
        }

image

@ajcvickers
Copy link
Member

@ansarizafar Scaffolding for nullable reference types is tracked by #15520.

@ajcvickers
Copy link
Member

Notes from team discussion:

  • We will provide an option to not generate OnConfiguring at all
  • This should be coupled with clear guidance on using the name= syntax

@ajcvickers
Copy link
Member

Linking to #19899, which is about the name= part.

@lajones lajones added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed poachable labels May 27, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview6 Jun 1, 2020
@bricelam bricelam removed their assignment Jul 26, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview6, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants