ef core 2.0 scaffold-dbcontext custom namespace - entity-framework

I am using the ef core 2.0 CLI command scaffold-dbcontext to reverse engineer poco classes from an existing DB (database first). I want to put the generated classes from scaffold-dbcontext in a "Model" folder within my project but I want the namespace of the classes to be something different than "MyProjectName.Model". For example, I want the scaffold-dbcontext generated files to live in the Model folder but I want the namespace of the genereated files to be "MyProjectName.Entities".
Is there a way to control the generated namespace with the scaffold-dbcontext cli command?
(To give you a little background, I want to define the custom namespace in the generated pocos so I can have a separate set of partial classes in another folder which won't be overwritten by regenerating the model using scaffold-dbcontext).

Currently no way to specify namespace, but similar issue open for ef 2.1 milestone.
So I hope it will be fixed in the next version.

Related

Entity Framework Core: Automated DB scaffolding into a class library

Following this tutorial it is possible to scaffold a database context using Entity Framework Core via the command line.
Scaffolding has some strange requirements
it needs an entry point aka main method (no scaffolding into class libraries)
the complete project has to be compileable (before scaffolding)
In my current project I need the model classes in a "shared project" (not directly a class library). The shared project does not have an entry point.
(I currently scaffold a lot since DB engineer updates the database model a lot in a database first approach).
In order to create some automated scaffolding task script I planed to automate the following tasks:
create a new, empty & temporay dot net command line application (it is buildable and has an entry point)
add required packages
scaffold the database context and model classes
move the generated classes to the library/shared project
delete the temporary project
So far I managed to automate the first points.
But I can't figure out a way how to add the ItemGroup DotNetCliToolReference to the xml of the .csproj file.
Is there any dotnet cli command that lets me add the DotNetCliToolReference and not only packages and project-to-project references?
Any other hints for me, please?
Install EntityFrameWorkCore.Sqlserver and EntityFrameWorkCore.Tools from Nuget
In package Manager Console write:
Scaffold-DbContext "Data Source=.;Initial Catalog="dbName";Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project "Project's class library Name"
As of EF Core 2.1 the reference is included in the .NET Core SDK this means you don't have to add the reference to the temp project.
https://blogs.msdn.microsoft.com/dotnet/2018/05/30/announcing-entity-framework-core-2-1/
The dotnet-ef commands now ship as part of the .NET Core SDK, so it will no longer be necessary to use DotNetCliToolReference in the project to be able to use migrations or to scaffold a DbContext from an existing database.
I have created a quick PowerShell script that is added to the classlib which then creates a temp project and scaffolds the dbcontext and cleans everything up at the end.
param(
[Parameter(Mandatory=$true)][string]$username,
[Parameter(Mandatory=$true)][string]$password,
[Parameter(Mandatory=$true)][string]$datasource,
[Parameter(Mandatory=$true)][string]$catalog,
[Parameter(Mandatory=$true)][string]$contextName
)
$workingDir = $PSScriptRoot;
Write-Host "Project dir= $workingDir";
Write-Host "Creating temp project to run scaffold on";
cd ..
mkdir temp
cd temp
dotnet new web
Write-Host "Done creating temp project";
Write-Host "Scaffolding dbcontext into project";
dotnet ef dbcontext scaffold "data source=$($datasource);initial catalog=$($catalog);user id=$($username);password=$($password);MultipleActiveResultSets=True;App=EntityFramework" Microsoft.EntityFrameworkCore.SqlServer --output-dir Entities --context $contextName --project temp.csproj
Write-Host "Done scaffolding dbcontext into temp project";
New-Item -ErrorAction Ignore -ItemType directory -Path "$workingDir/Entities";
Move-Item ./Entities/* "$workingDir/Entities" -force;
Write-Host "Scaffold completed! Starting clean-up";
cd ..
Remove-Item ./temp -force -Recurse;
cd $workingDir;
Write-Host "Clean-up completed!";
I'm sure the script can be improved but it works great as it is.
The script works as follows:
Let's say we run the script in the classlib directory: c:/test/classlib
Store the current directory
Create a temp web project: c:/test/temp
Scaffold the DBContext
Move the scaffolded items to the classlib (Be aware the script is set up to override the current Entities dir)
Delete the temp project
You would still have to change the namespace to the correct one. Support for namespace on scaffold is currently on the list for 2.2 as far as I know. Hope it helps!
Have you tried my "EF Core Power Tools" - they may be able to help you automate the process?
To add another project as a reference to current use dotnet add reference:
dotnet add reference lib1/lib1.csproj
For packages use dotnet add package:
dotnet add package Newtonsoft.Json

revEng command in Entity Framework 7 creates file in the project root

I am kicking the tires with EF 7, specifically the revEng command.
I have been able to run the revEng command and get the context and POCO files generated.
However there are two things I would like to tweak:
The context and POCO files are created in the project root.
The database connection string is hard coded on the context file.
Is there a way to move the file to another folder from the command? In other words, a Models folder? I could move the files manually, but doing that each time I update the model sounds like it would get old.
Is there a way to have the generated context file reference a connection string from the config.json?
If what I ask is not available, is it in the backlog, and this just life in preview land?
The abibilty to specify a project folder will be available in beta 8 https://github.com/aspnet/EntityFramework/commit/5b19fbbff82987ba9e1aafe051ff8c4fd02bf8cf

EF 4.3 migrations throwing "Unable to open configSource file"

I'm trying to use EF 4.3 migrations feature. My ASP.NET MVC project stores connection strings in external file:
<connectionStrings configSource="bin\connections.config" />
All runtime procedures (including automatic migrations) work fine. However, no powershell commandlet, connecting to the database, is able to find external file. It throws "Unable to open configSource file" exception. I was trying to place .config file in different places as well as changing configured external file location to no avail. Is there any workaround available?
Update: I've found that EF creates a temporary AppDomain with configuration file located in temp directory. So the only workaround at the moment, it seems, is to place external configuration in the same temp directory. Any other suggestions?
Using EF 6.1 here.
If like me you were linking to a connectionStrings.config file located in another project than your Entity Framework migrations project (using Add as link), you'll probably need to move the file back to this EF project and link to the moved file from the other projects instead...
There are unfortunately no easy way to handle external configSource files with the powershell cmd-lets in EF migrations. I've given up on it and moved the connection strings into the config file for the class library that contains the db code. The alternative is, as you've found out yourself to manually copy the file. Unfortunately the copy process doesn't honor the build settings of the project, so setting the external config file to be copied at build doesn't help.
EF 4.3.1 supports configSource.

Doctrine 2 : orm:generate-repositories not working

I recently integrated zend framework with doctrine 2. I have some issues with generating repository classes. I have no problems generating proxies.
The weird part is when I generate-repositories the output says
Repository classes generated to "/library"
But I cant seem to find the files. Inside my Entity class I have:
#Entity(repositoryClass="Entity\Repository\UserRepository")
Does anyone have any idea?
I don't know what is the directory structure you use, but according to most of the guides I found online and the way I implemented it, I guess your Entity directory is inside somesort of a MyApp directory which is inside the library directory.
If this is the case indeed, your #Entity line should be:
#Entity(repositoryClass="MyApp\Entity\Repository\UserRepository")
as opposed to what you wrote
#Entity(repositoryClass="Entity\Repository\UserRepository")
where MyApp is your model's root namespace name.
The #Entity line you wrote, should have generated the repository files in library/Entity/Repository.
Are you running the doctrine script with the correct path? This is how run it and it created the repos in the correct place
php scripts/doctrine.php orm:generate-repositories library/
I think you are using /library instead of library/
just a thought.
PS: I run this in one level above the document root.

Entity model assembly build on command line

Is there an alternate way of building an entity framework assembly (i.e. with csdl, storage and mapping resouces) at the command line (i.e. csc etc) without the use of msbuild. Our build process and environment is a bit out of date.
Using msbuild instead...
I'm not 100% sure but If you do need to use csc you can use the /resource switch to embed files in the output assembly.