4

I've got a web application written in .NET Core using Entity Framework to connect to a SQL Server database. Everything works fine locally. The staging/production environment for the app is AWS Elastic Beanstalk, and I seem to be stuck on how to get the app to connect to the database there.

I've created an RDS (SQL Server) database underneath the Elastic Beanstalk app.

Connection string:

user id=[USER];password=[PASSWORD];Data Source=[SERVER].rds.amazonaws.com:1433;Initial Catalog=Development

Creating the DbContext:

services.AddDbContext<MyDbContext>(o =>
                o.UseSqlServer(Configuration["Db:ConnectionString"]));

App fails to start, based on the following line in Startup.cs:

dbContext.Database.EnsureCreated();
Liam Smith
  • 152
  • 9
  • Will you post the exact exception or do you want us _guess:_ what error you might get? – Tseng Jul 11 '17 at 15:56
  • I'm having some trouble getting a specific error message (I'm a bit new to AWS). I assume it can't connect to the database, but how would I get more information. I didn't see anything in the application's logs. – Liam Smith Jul 11 '17 at 16:05
  • How do you run you rapp? When you host it in IIS you can use the web.config to enable logging (if console output is on). On linux a quick around is to pipe the console output to a file. Or install a proper logging provider – Tseng Jul 11 '17 at 16:07
  • Or do a remote debugging session: https://blogs.msdn.microsoft.com/devops/2017/01/26/debugging-net-core-on-unix-over-ssh/ – Tseng Jul 11 '17 at 16:09
  • I would have thought there should be a blog about this somewhere but google didn't get me any. If you have found a solution, please share. Also, did you try to ask AWS support? – dragonfly02 Jul 24 '17 at 20:56
  • did you try `dbContext.Database.Migrate()` instead of `EnsureCreated()`? – dragonfly02 Jul 25 '17 at 13:58
  • It's not exactly clear what the problem you are having is. Are you properly Migrating before running EnsureCreated? Are you hardcoding the properties of the connection string in your Config file? Did your RDS database change since doing this? There's a more dynamic way to do that, see my answer here: https://stackoverflow.com/a/45408863/311444 – Pinski Jul 31 '17 at 18:25
  • I ran into a similar issue and couldn't get error messages because the app never "starts up" when it exceptions in Startup.cs. I had to enable stdout and write to the console. https://learn.microsoft.com/en-us/aspnet/core/hosting/aspnet-core-module I found that none of the standard directories on the EC2 instance worked for me for log files. I ended up just making a directory in c:\Logs\ . Make sure that you create the directory first as IIS won't do that for you. Also, make sure the stdoutLogFile="c:\Logs\" has a slash at the end so it doesn't try to make files on the C drive. – Pinski Jul 31 '17 at 18:26

1 Answers1

0

You have to troubleshoot step by step as below procedure:

  1. Db Connection string is working or not? Better to use with other app and simple doing the Db Connection testing. It could be possible that firewall block your port 1433.
  2. As per your codes, .NET Core Framework will crate a database by code first approach. So that, you have to make sure, your IIS Application Pool user account has write access to SQL Database. Most probability it could be your problem.
Zin Min
  • 3,898
  • 1
  • 20
  • 24