0

I am a new web developer and am trying to host a test site with Azure test services.

I can see the test site(you can access this to test ) at: http://kencast20160830102548.azurewebsites.net/

However, if you go to the Services -> Fazzt --> Equipment and Applications pages, I get this error:

Error. An error occurred while processing your request. Development Mode Swapping to Development environment will display more detailed information about the error that occurred.

Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable toDevelopment, and restarting the application."

These pages are relying on a SQL database, so I think this is where the problem is.

I've been trying to follow along with the directions published here: https://docs.asp.net/en/latest/tutorials/publish-to-azure-webapp-using-vs.html

however I cannot find the "Configure SQL Database" pop-up box when logged into my Microsoft Azure account.

The directions do not seem to go along with what exists in Azure.

Update- 8/31/2016

I have researched and learned a bit more: I have one project with two DBContexts.

When I publish to Azure, it publishes tables from ApplicationDBContext but not the tables from MyCompanyContext. I can verify using SQL Server Object Explorer. I can see my local connection strings in appsettings.json file for both ApplicationDB and MyCompanyDB. Here is the code from appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MyCompany-3097a012-5e00-4c25-ad83-2730b4d73b4b;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Data": {
    "MyCompanyContext": {
      "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=MyCompanyContext-f9de2588-77e8-41dd-abb3-84341610b31a;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  }

}

However, when I look at the "SQL Server Object Explorer" window, I see that the database hosted on Azure, mycompanydb.database.windows.net(SQL Server 11.0.9231 -mycompanydb, MyCompany_db) only has the tables from the "DefaultConnection" database, and nothing from "MyCompanyContext".

How do I get the tables from the second database (MYCompanyContext) to Azure?

I have been studying this Stack Overflow response, but it uses Enable-Migration in the PMC. When I do that, I get an error that enable-migrations is obsolete.

Locally, I have always done migrations with this: add-migrations -c MyCompanyContext

Any help you could give me would be greatly appreciated.

Thanks!

Community
  • 1
  • 1
Kelsey Steele
  • 219
  • 1
  • 2
  • 7
  • The configure sql shows up if you're connecting to SQL using EF and that the wizard identifies an existence of DBContext class (atleast that is what happens with my VS 2015). So to get your app to connect to the SQL Db, you may be reading the connection string in what i called a `Startup.cs` if you have it stored in appsettings.json file . https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html. Then publish the web app to azure as shown in the link you posted – Jaya Aug 30 '16 at 20:40
  • @JS_GodBlessAll Thank you for responding, but I don't understand. What is EF? It was mentioned in the link that was included in your comment. I also don't understand what I should do from this comment: "So to get your app to connect to the SQL Db, you may be reading the connection string in what i called a Startup.cs if you have it stored in appsettings.json file . docs.asp.net/en/latest/tutorials/first-mvc-app/…" This link seems to be about running a SQL Database on a Local DB. I want to run it on Azure. Thanks! – Kelsey Steele Aug 30 '16 at 21:45
  • np, EF = entity framework ; to connect to SQL Db be it on azure or local - you have to configure your connection string appropriately. Wherever it says local, point it to your SQL server up on the cloud. Startup.cs is the entry point for your .net core app. Here are some of the useful links to get you started https://docs.asp.net/en/latest/data/index.html; http://stackoverflow.com/questions/31097933/setting-the-sql-connection-string-for-asp-net-5-web-app-in-azure – Jaya Aug 30 '16 at 22:05
  • At what point to you deviate from the tutorial? (Also: I am not so sure this is SQL server related at all; development environment warnings like this fit more with a web-app framework than SQL Server. ) – Dan Rediske Aug 30 '16 at 22:18
  • @drediske I've repeated the tutorial twice and because I am working with an existing project, I do skip the part about creating a web app. I also have not seen a screen about applying migrations after registering the project. I am signed in with an individual user account. – Kelsey Steele Aug 31 '16 at 14:05
  • @JS_GodBlessAll I have researched and learned a bit more: I have one project with two DBContexts. When I publish to Azure, it publishes tables in ApplicationDBContext but not the tables for MyCompanyContext. I can verify using SQL Server Object Explorer & see my local connection strings in appsettings.json file for both databases. How do I get the tables from the second database to Azure? – Kelsey Steele Aug 31 '16 at 15:30
  • Kelsey- update your question with your progress and the code you think is most relevant (sans any sensitive material). I think I've seen this issue before, but am unsure on the specifics to help you out. – Dan Rediske Aug 31 '16 at 17:32
  • @drediske, I've updated my question. – Kelsey Steele Aug 31 '16 at 18:09

1 Answers1

1

From the comments, I am guessing you need to ensure both your contexts are applied in the startup class like this

  services.AddEntityFramework().AddSqlServer()
            .AddDbContext<MyCompanyContext>(options => options.UseSqlServer(Configuration.Get("Data:SQL")))
            .AddDbContext< ApplicationDBContext >(options => options.UseSqlServer(Configuration.Get("Data:SQL")));

the above assumes two things: 1) you have the connection string set up like this in your appsettings.json

"Data": {
"SQL": "Server=tcp:yourDbServer.database.windows.net,1433;Initial Catalog=yourDb;Persist Security Info=False;User ID=youId;Password=YourPswd;MultipleActiveResultSets=True;TrustServerCertificate=False;Connection Timeout=30;",
}

2) Both the contexts share the same Db, if not then substitute the appropriate connection string (make sure to have it matched to the way you have it in your appsettings.json) for the context like this sample:

.AddDbContext<MyCompanyContext>(options => options.UseSqlServer(Configuration.Get("Data:SQL2")));
Jaya
  • 3,721
  • 4
  • 32
  • 48
  • Both context look like they are applied in the startup class on my local computer and the connection strings look like what you suggested. I added a "SQL" line as you suggested to appsettings.json, but that did not solve anything. I logged onto kudo dashboard of Azure, and they look the same on there as well. I did see a docment called "appsettingsProduction.json" and that looked like this: – Kelsey Steele Aug 31 '16 at 20:00
  • okay, again am guessing cause you came back with new info saying there are two appsettings.json file - if so - then do the following: 1) Check on AzurePortal what is the `ASPNET_ENVIRONMENT` looks like meaning - is it set to `development` or `production`. Also check for lines of code on `startup.cs` to see if it has conditional loading of appsettings, meaning : `.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true);` if so check if the connection strings that correspond to both the DbContext are in there? debug locally to see if both are available? Lastly - is it working locally? – Jaya Aug 31 '16 at 21:36
  • I cannot find the ASPNET_ENVIRONMENT on the AzurePortal/Kudu Dashboard, but on my local computer the ASPNET_ENVIRONMENT is set to "development". Everything else looks right and the connection strings contain both of the DbContext. It is definitely working locally and both DbContext are working. – Kelsey Steele Sep 01 '16 at 15:28
  • So if ASPNET_ENVIRONMENT is not set in Azure's web portal - AppSettings , then how do you know which one of those `appsettings.json` file is used? what do you see here? https://myWebAppName.scm.azurewebsites.net/Env ; then--> Steps to ensure you have indeed the right connection string, login to your Azure portal - Navigate to the web app blade ->AppSettings-> then underneath Connection string place SQL as the key and put in the connection string you want in the value, *Do not forget to hit save at the top* then test your app. – Jaya Sep 01 '16 at 18:57
  • Did this fix the issue? – Dan Rediske Sep 06 '16 at 21:25