EF6 How to specify directory for add-migration? - entity-framework-6

On VS2019, C#, EF6, using the Package Manager Console, I am trying to specify the -OutputDir for the Add-Migration command according to this link but it always writes to the root of the project. If I remove the -OutputDir param, it will write inside the \Migrations folder but I prefer to be inside \Migrations\MySubFolder
PM> add-migration -name CreateInitialTables -OutputDir \Migrations\MySubFolder -context MyDbContext

From the docs for Add-Migration:
-OutputDir <String>: The directory use to output the files. Paths are relative to the target project directory. Defaults to "Migrations".
Because the path is relative, you need to specify the path without the leading slash, or use .\ to indicate the current directory. For example:
add-migration -name CreateInitialTables -OutputDir .\Migrations\MySubFolder -context MyDbContext

For people using the dotnet ef migrations command, you can use the --output-dir argument to do this.
From dotnet ef migrations add --help:
...
Options:
-o|--output-dir <PATH> The directory to put files in. Paths are relative
to the project directory. Defaults to "Migrations".
...
For example:
dotnet ef migrations add InitialCreate --context MyContext --output-dir Migrations\MyContext

Related

How to integrate Hybris Custom and Config folder to local installation?

I installed Hybris 1905 out of the box locally on my machine and I would like to include the custom code which our project stores on bitbucket.
To track the progress of the commits, I installed earlier Sourcetree and have integrated the repository. The repo is stored locally in directory
C:\git\projectname
How can I now get the folders custom and config into my local installation to be able to run always the latest code in my local machine? Thanks!
You can create a symlink or directory junction.
Assuming your custom config is stored in C:\git\projectname\config and your custom code is stored in C:\git\projectname\bin\custom
In hybrisHomeDirectory, open command prompt then type :
mklink /J config C:\git\projectname\config
this will create a directory junction that link your config to hybris installation folder.
In hybrisHomeDirectory/bin, open command prompt then type :
mklink /J custom C:\git\projectname\bin\custom
This will create a directory junction that link your custom code.
If you want to create a symlink instead of directory junction (requires admin), then open cmd as administrator and type mklink /D instead of mlink /J
Config folder
In order to refer a config folder(other than the default one) you can edit your hybris\bin\platform\setantenv.bat. For example please have a look at my setantenv.bat that I used in the past:
#echo off
set ANT_OPTS=-Xmx2g -Dfile.encoding=UTF-8 -Djdk.util.jar.enableMultiRelease=force
set ANT_HOME=%~dp0apache-ant
set PATH=%ANT_HOME%\bin;%PATH%
rem deleting CLASSPATH as a workaround for PLA-8702
set CLASSPATH=
Rem Custom zone start
set HYBRIS_RUNTIME_PROPERTIES=%~dp0local_custom.properties
chcp 65001
set HYBRIS_CONFIG_DIR=%~dp0..\..\..\gitrepo\config
echo -------CustomChanges--------
echo CustomChanges: configFolder: %HYBRIS_CONFIG_DIR%
echo CustomChanges: runTimeProperties: %HYBRIS_RUNTIME_PROPERTIES%
echo -------CustomChanges--------
Rem Custom zone end
echo ant home: %ANT_HOME%
echo ant opts: %ANT_OPTS%
ant -version
Between Rem Custom zone start and Rem Custom zone end there is the Custom section that, among other things, sets the path to the config folder.
In my case, hybris folder and gitrepo folder are right next to eachother(in the same parent folder) and that is why the following path(also mentioned above) works:
set HYBRIS_CONFIG_DIR=%~dp0..\..\..\gitrepo\config
Keeping these two folders next to each other makes it easier to use relatives paths in order to easily use resources from git Repo into Hybris.
Custom folder
In order for Hybris to take into consideration the custom extensions, their folder needs to be specified in the localextensions.xml as exemplified below:
<path autoload="true" dir="${HYBRIS_BIN_DIR}/../../gitRepo/extensions"/>
Again, above relative path works for me because hybris and gitrepo folders are next to each other.

No DbContext found in Console App using EF Core dotnet EF migration add results in No DbContext found

Please note this is a CONSOLE app.
The Problem
When using dotnet ef migrations add InitialCreate --context Efc.Models.ApplicationDbContext I receive
the following message: "No DbContext named 'Efc.Models.ApplicationDbContext' found"
I am running the dotnet command in the EFConsole.ConUI directory. The spellings seem to be correct.
Using VS2019, EntityFramework Core 3.1.3
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Server=SSSSSSSS\XXSQLSERVER;Database=PhoneNumberTest;User ID=XXXXXXXX;Password=XXXXXXXX");
}
The following packages are installed as indicated:
EFConsole.ConUI Console project
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore
Efc.Models -- class library for models and ApplicationDbContext
Microsoft.EntityFrameworkCore.SqlServer
Efc.RepositoryLayer -- class library for the repositories.
Microsoft.EntityFrameworkCore.SqlServer
If I move the ApplicationDbContext to the EFConsole.ConUI project the dotnet ef command works. But for obvious reasons the ApplicationDbContext needs to be in a separate project so that it can be referenced from the Efc.Repository project at a minimum.
EDIT:
Note: Because this is a console APP I do not have a Startup class and I have not loaded any services like one would in a WebApp in its Startup class. Could this be the problem??
I have searched and read until my eyes were bleeding. :(
Thanks for any guidance.
Try opening the Nuget Package Manager Console:
In Visual Studio: Tools -> NuGet Package Manager => Package Manager Console
Select the default project to the one where the DbContext class is:
Run the command:
add-migration InitialCreate

How to use ef6.exe

I'm a newbie in entity framework. I have seen examples of using migrate.exe or enable-migration etc.
But migrate.exe does not exist any more. I explored the web and found that ef6.exe replaced the old migrate.exe
so I tried
ef6 -contexttypename musicstoredatacontext
ef6 -context musicstoredatacontext
nothing worked
I cannot find further support/documentation regarding the migration commands. and i'm new to migration.
Can somebody give me some hint how to work on it?
"If you execute Update-Database -Verbose inside Visual Studio's Package Manager Console, it will show you the call it uses to ef.exe to apply migrations." -Brice Lambson
It will be something like:
<PATH_TO_EXE>\ef6.exe database update --connection-string "<CONNECTION_STRING>" --connection-provider System.Data.SqlClient --verbose --no-color --prefix-output --assembly <PATH_TO_DLL> --project-dir <PATH_TO_PROJECT_DIR> --language C# --data-dir <PATH_TO_APP_DATA> --root-namespace <NAMESPACE> --config <PATH_TO_WEB_CONFIG>
Some of the arguments above can be omitted.
You can use the same strategy to determine how to create a migration (Add-Migration) with ef6.exe.
SOURCE: https://github.com/dotnet/ef6/issues/1365#issuecomment-540067758
use the command help to get the docs
ef6 --help
or
ef6.exe migrations --help
I succeeded in adding migration by updating powershell and use add-migration initial command
Reference can be found here.
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

Code First Migrations - File Name

We are using EF code first migrations but having an issue with the filename that gets created. We notice that it is appending the date and time to the beginning of our migration name when scaffolding changes and creating a new migration. This causes our migrations to be out of order when having to add a patch migration (in release) and then merge that into our dev branch. IE the patch will be after the new migrations in the dev branch due to the date in the file name, but we need them to appear and be ran in a specific order.
Is there any way to turn off the date that gets appended to the beginning of the migration file name and have it just use the actual name of the migration?
I've tried renaming the file and the reference to the name in the designer but that doesn't work because it prompts me to re-scaffold the changes again.
Anyone have any suggestions?
Thanks!
one way to get rid of this naming is somehow to add your own naming automatically. I used to use the command prompt file (.cmd) to create a name automatically (including date and time). Doing so I just need to double-click the .cmd file and the migration with my own automatic file name will be performed.
here is my .cmd file content (for example add_migrations_Identity.cmd):
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%a_%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME: =0%") do (set mytime=%%a%%b)
dotnet ef --startup-project MyProject migrations add Identity_V%mydate%_%mytime% -o Migrations/Identity -c IdentityContext
pause
and here my update .cmd file (as an example update_db_Identity.cmd):
dotnet ef --startup-project MyProject database update -c IdentityContext
pause

EF Core Migrations in Separate Project - Issues

I am not successful in adding ef core migrations in a separate project. I have provided the project structure at the end.
The DbContext class and the migrations are in separate projects such that the former is a class library and the migrations project is a console application. I added a reference to the DbContext project in the migrations project however, I am getting errors while running migration commands
\EF.BlogsDb.Migrations > dotnet ef migrations add InitialCreate --project EF.BlogsDb.Migrations.csproj
No DbContext was found in assembly 'EF.BlogsDb.Migrations'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
I followed advise from this link but I am not sure of the following.
In which project should i add these lines? is it the migrations project or my web project or the DbContext project?
options.UseSqlServer(
connectionString,
x => x.MigrationsAssembly("MyApp.Migrations"));
Why should i Add a reference to the migrations assembly from the startup assembly?
From which location/folder/project should i run this command?
dotnet ef migrations add NewMigration --project MyApp.Migrations
I can share the source code if you can tell me how to do that.
Thanks in advance for the clarifications one might provide!
Here is my project structure looks like:
The issue is solved using the following command.
dotnet ef --startup-project ../Project.Api/ migrations add InitialModel
Here I am using separate folder for the api and the EF.
Run the above command from the EF project folder
Start up project means referred to the api

Resources