An issue that an ASP. NET Core project might encounter when migrating with entity Framework Core 2.0 on a Mac.

Source: Internet
Author: User
Tags dotnet

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.