Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Update database with migrations using development environment settings (appsettings.Development.json) #6704


User avatar
0
piapps created

Hi guys,

I have added an appsettings.Development.json file to my Web.Host project for use on my local machine which works fine.

However the EntityFrameworkCore project must not use the file because whenever I do Update-Database it fails (unable to find sql instance) unless I put the correct conn string back into appsettings.json.

How or where can I adjust the connection string resolver so that .Development will be respected by migrations?

Im using v6.8.0

Regards, Pete


13 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    You can try this solution: https://entityframework.net/knowledge-base/48913968/net-core-2---entity-framework--update-database-of-different-environment

    Related discussion: https://github.com/aspnet/EntityFrameworkCore/issues/7353

  • User Avatar
    0
    piapps created

    Neither using set or $env = seemed to work, any other ideas?

  • User Avatar
    0
    ismcagdas created
    Support Team
  • User Avatar
    0
    piapps created

    Hi again,

    Sorry I should have been clearer in my response yesterday.

    I have tried both the following and they dont work

    set ASPNETCORE_ENVIRONMENT=Development

    and

    $env:ASPNETCORE_ENVIRONMENT='Development'

  • User Avatar
    0
    maliming created
    Support Team

    @piapps

    You can try modifying the DbContextFactory as follows:

    Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")

    public class AbpZeroTemplateDbContextFactory : IDesignTimeDbContextFactory<AbpZeroTemplateDbContext>
    {
    	public AbpZeroTemplateDbContext CreateDbContext(string[] args)
    	{
    		var builder = new DbContextOptionsBuilder<AbpZeroTemplateDbContext>();
    		var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder(), Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"), addUserSecrets: true);
    
    		AbpZeroTemplateDbContextConfigurer.Configure(builder, configuration.GetConnectionString(AbpZeroTemplateConsts.ConnectionStringName));
    
    		return new AbpZeroTemplateDbContext(builder.Options);
    	}
    }
    
    PM> $Env:ASPNETCORE_ENVIRONMENT = "Staging"
    PM> $Env:ASPNETCORE_ENVIRONMENT
    Staging
    PM> Update-Database
    Applying migration '20170406083347_Initial_Migration'.
    Applying migration '20170623075109_AspNetZero_V4_1_Changes'.
    Applying migration '20170704084731_Added_GoogleAuthenticatorKey_Column'.
    Applying migration '20170714081027_Added_Relation_Between_Edition_And_SubscriptionPayment'.
    Applying migration '20170724142223_Upgraded_To_Abp_V2_2'.
    Applying migration '20170913133916_Added_SharedMessageId_To_ChatMessage'.
    Applying migration '20170914070123_Added_ReceiverReadState_To_ChatMessage'.
    Applying migration '20170914084815_Invoice_Changes'.
    Applying migration '20170914121022_TypeChange_SharedMessageId_String_Guid'.
    Applying migration '20180118065703_Added_Entity_History'.
    Applying migration '20180320065710_Upgraded_To_Abp_V3_5'.
    Applying migration '20180718081610_Abp_v3_7_Changes'.
    Applying migration '20180726063233_Upgraded_ABP_v3.8.0'.
    Applying migration '20180731052649_Upgrade_ABP_v3.8.1'.
    Applying migration '20180807062930_Upgrade_ABP_v3.8.2'.
    Applying migration '20181012141151_Upgraded_To_Abp_v3_9_0'.
    Applying migration '20190103081952_Recurring_Payment_Changes'.
    Applying migration '20190208083524_Upgraded_To_Abp_v4_2_0'.
    Applying migration '20190304131651_Added_User_OrganizationUnits'.
    Done.
    PM> 
    
  • User Avatar
    0
    piapps created

    That worked thanks, better yet after setting that change to the factory I have gone into properties of the EFCore project and set the environment variable in there so it remembers between Visual studio sessions (same as the host project)

    Thanks again

  • User Avatar
    0
    murat.yuceer created

    Thanks maliming why this code not added default template

  • User Avatar
    0
    maliming created
    Support Team

    I will consider adding this code to the template. : )

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because of no recent activity. Please open a new issue if you are still having this problem.

  • User Avatar
    0
    conzepta created

    Hello

    @maliming, have you considered adding this code to the template? I mean, it was 2 years since this bug was reported and 1 year since you said, that you will consider it.

    Today I ran in the exact same issue and it cost me quite some time to finally find this thread. As one might imagine, it is quite annoying to spend time investigating a 2 year old bug, that already has a solution but the developers were just considering fixing it, instead of actually fixing it...

    So could someone please finally add this fix to the template?

    Thanks a lot

    Kind regards

  • User Avatar
    0
    maliming created
    Support Team

    hi @conzepta

    I will add some comment to IDesignTimeDbContextFactory

  • User Avatar
    0
    maliming created
    Support Team

    https://github.com/aspnetzero/aspnet-zero-core/pull/3764

  • User Avatar
    0
    conzepta created

    Thank you very much =)