EF Core CLI Migration Database Update Flow

96 Views Asked by At

May I know how does the actual flow look like when performing migration using the command below?

dotnet-ef database update --project MyModel --startup-project MyProject --context DBContext --configuration UAT -v

It is always out of my expectations. When I run the command above, it will keep performing migration on my dev database. I have a static class that will set my DB connection string on the MyProject.Program.Main using the preprocessor directives. Like the code below.

#if DEV
Setting.Env = "DEV";
#elseif UAT
Setting.Env = "UAT";
#else
Setting.Env = "DEV";
#endif

In my static Setting class have the function like below:

public static string GetDBConn()
{
    switch (Env)
    {
        case "UAT":
             return UATDBConn;
        default:
             return DEVDBConn;
    }
}

FYI DEVDBConn and UATDBConn are read-only strings in the Setting static class.

I also set the DBContext on the Startup.ConfigureServices as below:

services.AddDbContext<DBContext>(
    options =>
    {
     options.UseSqlServer(Setting.GetDBConn());            
    });

Do I miss something to make my migration command work correctly?

Any help is greatly appreciated.

1

There are 1 best solutions below

0
Akshay Kumar On

You have define the elseif like that but it will be like that elif. you can check more about Preprocessor directives

#if DEV
Setting.Env = "DEV";
#elif UAT
Setting.Env = "UAT";
#else
Setting.Env = "DEV";
#endif