You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DNX Commands: Enable DbContext and Startup in different projects #2679
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.
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.
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.
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
@JustSayNO the recommended pattern in ASP.NET applications is to configure the DbContext on Startup.ConfigureServices(), e.g. like one of the snippets above:
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.:
@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?
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:
@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.
@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.
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.
Activity
bricelam commentedon Jul 22, 2015
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.
analogrelay commentedon Jul 22, 2015
Yeah, this would probably be best tracked via an issue in DNX to enable global commands to operate in project context.
rowanmiller commentedon Jul 24, 2015
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.
bricelam commentedon Sep 18, 2015
Re-opening. We need this to properly enable
--startupProject[-]Make ef a global command instead of a project command[/-][+]DNX Commands: Make ef a global command (enables --starupProject)[/+]8 remaining items
sethreidnz commentedon Nov 24, 2015
How do you then access the default connection string from the override of OnConfiguring required to set up the data source. IE:
With my default connection string being in the MVC app and this code being in the DbContext class library.
divega commentedon Nov 24, 2015
@JustSayNO the recommended pattern in ASP.NET applications is to configure the DbContext on
Startup.ConfigureServices(), e.g. like one of the snippets above: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 theStartup.Configurationproperty is static, e.g.:Functionally there isn't any significant difference between configuring the DbContext in
Startupor inOnConfiguring().sethreidnz commentedon Nov 24, 2015
@divega Thank you!
fe-rod commentedon Jan 10, 2016
@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?
bogdandanielb commentedon Mar 17, 2016
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:
rowanmiller commentedon Mar 21, 2016
@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.bogdandanielb commentedon Mar 23, 2016
@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.
DevNeeraj commentedon Apr 27, 2016
@bricelam
dnx ef DbContext Scaffold "Server=databasename;Database=data;UserID=*;Password=*;" EntityFramework.MicrosoftSqlServerI have tried many things but still unable to find RevEng command under the ef.
In a project, i am using
and
bricelam commentedon Apr 28, 2016
@DevNeeraj, it's
dbcontext scaffold