I am not sure what I am doing wrong here, I want to use the connection string from appsettings.json and use DI to setup my DbContext for use.
{
"ConnectionStrings": {
"DarkwinterDatabase": "server=localhost;userid=user;pwd=pass;port=3306;database=Darkwinter;sslmode=none;"
},
...
}
In the ConfigureServices method in Startup.cs I have the following code.
services.AddDbContext<PostContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("DarkwinterDatabase")));
My DbContext has one DbSet and accepts DbContextOptions to pass to base to be initialized. This is where I think I am wrong but should DI pass options to this class?
public class PostContext : DbContext
{
public PostContext(DbContextOptions options) : base(options) { }
public DbSet<Post> Post { get; set; }
}
How ever when I try to use my Context I get an error: There is no argument given that corresponds to the required formal parameter 'options' of 'PostContext.PostContext(DbContextOptions)
using(var db = new PostContext())
{
var posts = db.Post.ToList();
return View(posts);
}
This makes sense since I didn't pass one in but I thought that the service should do that? I know I am thinking about this the wrong way and was hoping someone would take the time to explain it to me.
DbContextOptionstype?