How can I specify the connection string/database to use in EF Core add-migration command? - entity-framework-core

I have multiple appsettings.{env}.json configuration files but the add-migration command doesn't have a way to specify which configuration file to use and seems to use only the hard-coded Development environment file.

Related

ef core 2.0 scaffold-dbcontext custom namespace

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.

How to specify the folder to create SQL database using dotnet ef

I am using Dotnet EF Core, and have created migrations. When I run
dotnet ef database update
the database is created in my windows user folder.
My connection string is
"ConnectionStrings": {
"ContextConnection": "Server=(localdb)\\MSSQLLocalDb;Database=MyDb;Trusted_Connection=true;MultipleActiveResultSets=true;"
}
How do I get the database created in the project directory? (without having to have the full project pathname in the connection string) Using VS2015 I was able to include |DataDirectory| in the connection string.
These CLI commands can be run from the command line by navigating to the project directory
I think you are trying to run CLI commands directly from user profile hence database is created in your windows user folder.
First, navigate to your project directory by using simple dos commands (e.g. CD)
And then run these CLI commands-
dotnet ef [options] [command]
One of the option-
-p|--project <PROJECT> The project to target (defaults to the project in the current directory). Can be a path to a project.json or a project directory.
For more information refer this link-
https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html#dotnet-ef
See if this helps.

Updating database to Migration Configuration

I have pulled down a Visual Studio 2015 project created my another developer. Within the Migrations folder are several Migration Configuration files ...
201601081315335_AddedPersonEntities.cs
201601091532275_AddedDepartmentEntities.cs
201601101145137_AddedPayrollEntities.cs
I would like to update my database to the point of one of these Migration Configurations. However when I try this command ...
Update-Database -Verbose -StartupProjectName MyApp.Api -ProjectName MyApp.Data -ConfigurationTypeName 201601091532275_AddedDepartmentEntities.cs
I get the following error ...
The migrations configuration type '201601091532275_AddedDepartmentEntities' was not be found in the assembly 'MyApp.Data'.
I was expecting it would bring my database up to the same schema at the point that 201601091532275_AddedDepartmentEntities was created. Am I missing something?
Go to visual studio, select your MyApp.Data and check the "Show All Files".
Inside the migrations folder, see if there aren't migrations "outside" the project. If there is, then add them to the project with Right-Click > Include in project.
Do you use TFS?
It happens when you add something (File/Folder) inside a project in your solution and check-in your solution, and your colleague doesn't do correctly the merge on the .csproj file (Which contains all the information about the files and folders inside the project).
WAIT
Ok i think this isn't the problem.
You are specifying -ConfigurationTypeName: don't you want -target: instead?
-ConfigurationTypeName Is used to define the configuration class (Normally contains the seed method).
-target Specifies to where you want to update your database (From the current migration to that specific one, forward or backwards it works anyway).
And, do you insert the models inside MyApp.Data or MyApp.Models?

How to upload file to PostgreSQL database using flyway?

I use in Windows 7 IntelliJ IDEA 12, JDK 7, MyBatis, Spring 3 in order to create REST web application (Maven project with flyway-maven-plugin). I use Flyway in order to cope with sql migrations. Now I need to load some files to PostgreSQL 9.2 database. I've found this thread: https://dba.stackexchange.com/questions/1742/how-to-insert-file-data-into-a-postgresql-bytea-column
I'd like to use bytea_import from that thread. This custom function requires path to the uploaded file (it is in resources folder). How can I correctly set relative path to such file? What is considered as a current folder during migrations?
Not sure about bytea_import (if you get it working, let me know!), but you should be able to achieve this easily using Java-based migrations.
You can use Java-based migrations. If you still want to use SQL-based migrations, then use Flyway placeholders. Save required path in placeholder using *.pom properties. Example:
<flyway.placeholders.rtfPath>${project.build.outputDirectory}/rtf</flyway.placeholders.rtfPath>
Then use rtfPath in your SQL migration file in order to generate the full path to your uploaded file.

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.