I am trying to use entity framework core code first, with an external sql server database hosted on AWS RDS. My aim is for EF to create my model database/tables automatically the first time I use Swagger to create an object. Currently, I am trying to enable migrations for my project before I allow EF code first to create the databases. Although when I try to Enable-Migrations I get an error: "The context type 'FitnessTrackerAPIContext' was not found in the assembly 'FitnessTrackerAPI'." As you can see from the below picture, the 'FitnessTrackerAPIContext' is in the 'FitnessTrackerAPI' so I am not sure why I am getting this error.
I am not quite sure if all my settings for EF core are correct s this is my first time using it.
Context:
namespace FitnessTrackerAPI.Data { public class FitnessTrackerAPIContext : DbContext { public FitnessTrackerAPIContext (DbContextOptions<FitnessTrackerAPIContext> options) : base(options) { } public DbSet<FitnessTracker.UserData> Users { get; set; } public DbSet<FitnessTracker.WorkoutData> Workouts { get; set; } } }
I have added the following to Startup.cs:
services.AddDbContext<FitnessTrackerAPIContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FitnessTrackerAPIContext")));
My connection string in appsettings.json:
"ConnectionStrings": {"FitnessTrackerAPIContext": "Server=fitnessapi.clxnbxbuoxx1.eu-west-1.rds.amazonaws.com,1433;User ID=XXXXX;Password=XXXXX; Initial Catalog=fitnessapi; Database=fitnessapi; Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
Do I need to add any other code? In program.cs or so? This is program.cs currently:
namespace FitnessTrackerAPI { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } }
Any advice would be appreciated.