Azure is not creating database schema on publish of EFCore 2 application

azure c# ef-core-2.0 entity-framework entity-framework-core

Question

Publishing ASPNetCore app to Azure, with associated data DLL.

Azure is not picking up any databases on the publishing wizard, but is picking up the DbContext, and the connection string from DbContext.

I've declared the connection string and DbProvider (SqlServer) in the Startup.cs and DbContext, it's also declared as an environment variable within the service app.

The publishing wizard looks like this.

After publishing through the wizard the app service deploys successfully, but the APIs all hit 500 server errors.

The build output contains several messages, that to me look like successes.

Generating Entity framework SQL Scripts...

Executing command: dotnet ef migrations script --idempotent --out "./path"

Generating Entity framework SQL Scripts completed successfully

adding database .. connstr

Do I need to create migrations for an initial create of the application?

I've been using the Code First paradigm method in EFCore, and so I have EnsureDeleted and EnsureCreated in some initializers for testing, but these aren't called at runtime.

I also have an EnsureCreated within the Startup.cs.

            if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
            {
                services.AddDbContext<FFDbContext>(options =>
                        options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection")));
            }
            else
            {
                services.AddDbContext<FFDbContext>(options =>
                       options.UseSqlServer(local connections string));
            }

            // Automatically perform database migration
            services.BuildServiceProvider().GetService<FFDbContext>().Database.Migrate();

            var context = services.BuildServiceProvider()
                .GetRequiredService<FFDbContext>();
                context.Database.EnsureCreated();

I feel like if anything I'm over compensating for trying to create the db, any ideas?

Edit - SQL Error from Azure Insights

System.Data.SqlClient.SqlException:
   at System.Data.SqlClient.SqlConnection.OnError (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlInternalConnection.OnError (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.TdsParser.TryRun (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlDataReader.get_MetaData (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlCommand.ExecuteReader (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader (System.Data.SqlClient, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute (Microsoft.EntityFrameworkCore.Relational, Version=2.2.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)

1
0
4/28/2019 10:36:58 PM

Accepted Answer

You just need to change your DefaultConnection in both of below files:

appsettings.json and appsettings.Development.json

Change this

"ConnectionStrings": {
"DefaultConnection": "Server=(LocalDb)\\MSSQLLocalDB;Database=webapi;Trusted_Connection=True;MultipleActiveResultSets=true"
}

To

"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=WebApiEfCore;Trusted_Connection=True;"
}

run the below queries:

dotnet ef migrations add initial
dotnet ef database update

Also you can try using Visual studio too and see if it helps.

0
4/29/2019 5:15:54 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow