Skip to content

DNX Commands: Enable DbContext and Startup in different projects #2679

Closed

Description

@davidfowl

Today users need to run dnx . ef, it would be better if they could just run a global command on any project without having to do project modification. To do this properly we'd need to have a way to load a runtime environment to harvest information for loading EF things.

/cc @anurse

Activity

  1. bricelam commented on Jul 22, 2015

    @bricelam
    Contributor

    Exactly, it executes in the context of your project. I tried going down this path, but things got ugly fast. xUnit.net also tried to do it, but it doesn't work at all since it never loads the "target" project. The worlds of global commands and projects are currently just too separated to do this.

  2. analogrelay commented on Jul 22, 2015

    @analogrelay
    Contributor

    Yeah, this would probably be best tracked via an issue in DNX to enable global commands to operate in project context.

  3. rowanmiller commented on Jul 24, 2015

    @rowanmiller
    Contributor

    Closing as there is nothing actionable from the EF side at the moment. If the design of global commands changes such that the EF commands could make use of them then we will reconsider.

  4. bricelam commented on Sep 18, 2015

    @bricelam
    Contributor

    Re-opening. We need this to properly enable --startupProject

  5. added this to the 7.0.0 milestone on Sep 18, 2015
  6. changed the title [-]Make ef a global command instead of a project command[/-] [+]DNX Commands: Make ef a global command (enables --starupProject)[/+] on Sep 23, 2015
  7. modified the milestones: , 7.0.0 on Sep 23, 2015
  8. 8 remaining items

  9. sethreidnz commented on Nov 24, 2015

    @sethreidnz

    How do you then access the default connection string from the override of OnConfiguring required to set up the data source. IE:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer("Data:DefaultConnection:ConnectionString");
                base.OnConfiguring(optionsBuilder);
            }
    

    With my default connection string being in the MVC app and this code being in the DbContext class library.

  10. divega commented on Nov 24, 2015

    @divega
    Contributor

    @JustSayNO the recommended pattern in ASP.NET applications is to configure the DbContext on Startup.ConfigureServices(), e.g. like one of the snippets above:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options => 
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    }

    But if for some reason you need to do this in the OnConfiguring() method on the context itself, you are going to need to make the configuration available outside of Startup. The simplest way I know you can do that is by making sure the Startup.Configuration property is static, e.g.:

    public static IConfigurationRoot Configuration { get; set; }

    Functionally there isn't any significant difference between configuring the DbContext in Startup or in OnConfiguring().

  11. sethreidnz commented on Nov 24, 2015

    @sethreidnz

    @divega Thank you!

  12. fe-rod commented on Jan 10, 2016

    @fe-rod

    @divega I understand that the recommended pattern is to configure DbContext in the Startup.ConfigureServices method, but that forces me to add a dependency between the web project and the data access layer where the DbContext lives. I prefer the web app to know nothing about the DbContext. Or I might be thinking something wrong.

    I think @JustSayNO was dealing with the same thing. The static option for Configuration would make the dependency the other way around, am I right?

  13. bogdandanielb commented on Mar 17, 2016

    @bogdandanielb

    Is there any solution yet to decouple the data persistence from the presentation ?
    It seems the Startup.cs is only used in the web project and not in the class library. Basically we should be able to add all EF7 related dependencies in project.json from our class library and do EF setup in the library as well:

                services.AddEntityFramework()
                    .AddSqlServer()
                    .AddDbContext<ApplicationDbContext>(options =>
                        options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
                    .AddDbContext<SkillContext>(options =>
                        options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    
  14. rowanmiller commented on Mar 21, 2016

    @rowanmiller
    Contributor

    @bogdandanielb you don't have to wire things up in Startup.cs if you don't want to. You can absolutely just define a DbContext in a class library, setup the connection string etc. there (typically by overriding OnConfiguring), and then just create instances in whatever app happens to want to consume the context.

  15. bogdandanielb commented on Mar 23, 2016

    @bogdandanielb

    @rowanmiller That is correct but I won't be able to benefit from the dependency injection which is set in the ConfigureServices method from Startup.
    Thanks for your answer.

  16. DevNeeraj commented on Apr 27, 2016

    @DevNeeraj

    @bricelam

    I am doing reverse engineering in my project. because our project we follows database first approach.
    Through this command, i achieved reverse engineering but not with RevEng command.

    dnx ef DbContext Scaffold "Server=databasename;Database=data;UserID=*;Password=*;" EntityFramework.MicrosoftSqlServer

    I have tried many things but still unable to find RevEng command under the ef.

    In a project, i am using

     "commands": {
            "web": "Microsoft.AspNet.Server.Kestrel",
            "ef": "EntityFramework.Commands"
        }
    

    and

     "dependencies": {
            "EntityFramework.Commands": "7.0.0-rc1-final",
            "EntityFramework.Core": "7.0.0-rc1-final",
            "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
            "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
            "EntityFramework.Relational.Design": "7.0.0-rc1-final",        
    }
    

    So how can i enable RevEng command in dnx,
    Help me to out !

  17. bricelam commented on Apr 28, 2016

    @bricelam
    Contributor

    @DevNeeraj, it's dbcontext scaffold

  18. modified the milestones: , 1.0.0 on Oct 15, 2022
  19. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions