Open Closed

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


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


9 Answer(s)
  • 0
    maliming created

    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

  • 0
    piapps created

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

  • 0
    ismcagdas created
  • 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'

  • 0
    maliming created

    @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> 
    
  • 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

  • 0
    murat.yuceer created

    Thanks maliming why this code not added default template

  • 0
    maliming created

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

  • 0
    ismcagdas created

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