In the ASP. NET Core 2.0 project, I used the entity Framework Core 2.0 as an ORM.
Some people are accustomed to the database of the connection string written in Appsettings.json, some of the habit of writing dead in the program, some habits put it in the Launchsettings.json inside (only here, the migration command can not find the connection string it).
I'm used to writing the connection string as a system environment variable.
I am the project database of the connection string variable name is "MLH:SalesApi:DefaultConnection", on Windows 10, I set the environment variable, and then everything CLI command operation is good.
But on the Mac, I was having problems.
If I set the name of the environment variable like Windows 10:
Export mlh:salesapi:defaultconnection="server=localhost; Database=salesapi; User Id=sa; [Email protected]; Multipleactiveresultsets=true"export MLH:AuthorizationServer:DefaultConnection=" Server=localhost; Database=authorizationserver; User Id=sa; [Email protected]; Multipleactiveresultsets=true"
So when you run bash:
You will be prompted with an error because Bash does not support the variable name with a colon: .
1. After reviewing the Efcore, Asp.netcore documentation, and searching for so, I found the first bad solution:
Using the ENV command, it sets the environment variable and then follows the command later.
Then I'll "env environment variable dotnet EF command":
Env mlh:salesapi:defaultconnection="server=localhost; Database=salesapi; User Id=sa; [Email protected]; Multipleactiveresultsets=true"dotnet EF Database Update
The problem with this command is that the setting of this environment variable is only valid for the command that follows it ... So if you want to migrate again, you need to enter this command again:
So this method is not advisable.
2. After a closer look at the document, I found the ultimate solution (or the document looked not fine): Https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index? Tabs=basicconfiguration#configuration-by-environment
If the system does not support the environment variable name with a colon: then use two underscores instead of colons.
Then change the name of the environment variable:
mlh__salesapi__defaultconnection="server=localhost; Database=salesapi; User Id=sa; [Email protected]; Multipleactiveresultsets=true"mlh__authorizationserver__defaultconnection=" Server=localhost; Database=authorizationserver; User Id=sa; [Email protected]; Multipleactiveresultsets=true"
Then close bash and reopen bash:
Execute the dotnet EF command:
Ok.
An issue that an ASP. NET Core project might encounter when migrating with entity Framework Core 2.0 on a Mac.