Hi @DMO
In the Startup ConfigureServices method, you should register the database context like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<EcContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("EcContextconnectionstring")));
}
You can set the connection string in the appsettings.json file:
"ConnectionStrings": {
"EcContextconnectionstring": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-2;Trusted_Connection=True;MultipleActiveResultSets=true"
}
And the database context as below:
public class EcContext : DbContext
{
public EcContext (DbContextOptions<EcContext> options)
: base(options)
{
}
public DbSet<Movie> Movie { get; set; }
}
More detail information, refer to the following tutorials:
Part 4, add a model to an ASP.NET Core MVC app
Part 5, work with a database in an ASP.NET Core MVC app
Tutorial: Get started with EF Core in an ASP.NET MVC web app
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion