"ConnectionStrings": {
"MvcMovieContext": "Data Source=MvcMovie.db"
When the app is deployed to a test or production server, an environment variable can be used to set the connection string to a production SQL Server. For more information, see Configuration.
Visual Studio
Visual Studio Code / Visual Studio for Mac
SQL Server Express LocalDB
LocalDB:
Is a lightweight version of the SQL Server Express Database Engine, installed by default with Visual Studio.
Starts on demand by using a connection string.
Is targeted for program development. It runs in user mode, so there's no complex configuration.
By default creates .mdf files in the C:/Users/{user} directory.
Examine the database
From the View menu, open SQL Server Object Explorer (SSOX).
Right-click on the Movie
table (dbo.Movie
) > View Designer
Note the key icon next to ID
. By default, EF makes a property named ID
the primary key.
Right-click on the Movie
table > View Data
The SQLite website states:
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. SQLite is the most used database engine in the world.
There are many third party tools you can download to manage and view a SQLite database. The image below is from DB Browser for SQLite. If you have a favorite SQLite tool, leave a comment on what you like about it.
For this tutorial you use the Entity Framework Core migrations feature where possible. Migrations updates the database schema to match changes in the data model. However, migrations can only do the kinds of changes that the EF Core provider supports, and the SQLite provider's capabilities are limited. For example, adding a column is supported, but removing or changing a column is not supported. If a migration is created to remove or change a column, the ef migrations add
command succeeds but the ef database update
command fails. Due to these limitations, this tutorial doesn't use migrations for SQLite schema changes. Instead, when the schema changes, you drop and re-create the database.
The workaround for the SQLite limitations is to manually write migrations code to perform a table rebuild when something in the table changes. A table rebuild involves:
Creating a new table.
Copying data from the old table to the new table.
Dropping the old table.
Renaming the new table.
For more information, see the following resources:
SQLite EF Core Database Provider Limitations
Customize migration code
Data seeding
SQLite ALTER TABLE statement
Seed the database
Create a new class named SeedData
in the Models folder. Replace the generated code with the following:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using System;
using System.Linq;
namespace MvcMovie.Models;
public static class SeedData
public static void Initialize(IServiceProvider serviceProvider)
using (var context = new MvcMovieContext(
serviceProvider.GetRequiredService<
DbContextOptions<MvcMovieContext>>()))
// Look for any movies.
if (context.Movie.Any())
return; // DB has been seeded
context.Movie.AddRange(
new Movie
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M
new Movie
Title = "Ghostbusters ",
ReleaseDate = DateTime.Parse("1984-3-13"),
Genre = "Comedy",
Price = 8.99M
new Movie
Title = "Ghostbusters 2",
ReleaseDate = DateTime.Parse("1986-2-23"),
Genre = "Comedy",
Price = 9.99M
new Movie
Title = "Rio Bravo",
ReleaseDate = DateTime.Parse("1959-4-15"),
Genre = "Western",
Price = 3.99M
context.SaveChanges();
If there are any movies in the database, the seed initializer returns and no movies are added.
if (context.Movie.Any())
return; // DB has been seeded.
Add the seed initializer
Visual Studio
Visual Studio Code / Visual Studio for Mac
Replace the contents of Program.cs
with the following code. The new code is highlighted.
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using MvcMovie.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext")));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
var services = scope.ServiceProvider;
SeedData.Initialize(services);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Delete all the records in the database. You can do this with the delete links in the browser or from SSOX.
Test the app. Force the app to initialize, calling the code in the Program.cs
file, so the seed method runs. To force initialization, close the command prompt window that Visual Studio opened, and restart by pressing Ctrl+F5.
Update Program.cs
with the following highlighted code:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using MvcMovie.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("MvcMovieContext")));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
var services = scope.ServiceProvider;
SeedData.Initialize(services);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Delete all the records in the database.
Test the app. Stop it and restart it so the SeedData.Initialize
method runs and seeds the database.
The app shows the seeded data.
Previous: Adding a model
Next: Adding controller methods and views
The MvcMovieContext
object handles the task of connecting to the database and mapping Movie
objects to database records. The database context is registered with the Dependency Injection container in the Program.cs
file:
Visual Studio
Visual Studio Code / Visual Studio for Mac
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext")));
The ASP.NET Core Configuration system reads the ConnectionString
key. For local development, it gets the connection string from the appsettings.json
file:
"ConnectionStrings": {
"MvcMovieContext": "Data Source=MvcMovieContext-ea7a4069-f366-4742-bd1c-3f753a804ce1.db"
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("MvcMovieContext")));
The ASP.NET Core Configuration system reads the ConnectionString
. For local development, it gets the connection string from the appsettings.json
file:
"ConnectionStrings": {
"MvcMovieContext": "Data Source=MvcMovie.db"
When the app is deployed to a test or production server, an environment variable can be used to set the connection string to a production SQL Server. For more information, see Configuration.
Visual Studio
Visual Studio Code / Visual Studio for Mac
SQL Server Express LocalDB
LocalDB:
Is a lightweight version of the SQL Server Express Database Engine, installed by default with Visual Studio.
Starts on demand by using a connection string.
Is targeted for program development. It runs in user mode, so there's no complex configuration.
By default creates .mdf files in the C:/Users/{user} directory.
Examine the database
From the View menu, open SQL Server Object Explorer (SSOX).
Right-click on the Movie
table (dbo.Movie
) > View Designer
Note the key icon next to ID
. By default, EF makes a property named ID
the primary key.
Right-click on the Movie
table > View Data
The SQLite website states:
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. SQLite is the most used database engine in the world.
There are many third party tools you can download to manage and view a SQLite database. The image below is from DB Browser for SQLite. If you have a favorite SQLite tool, leave a comment on what you like about it.
For this tutorial you use the Entity Framework Core migrations feature where possible. Migrations updates the database schema to match changes in the data model. However, migrations can only do the kinds of changes that the EF Core provider supports, and the SQLite provider's capabilities are limited. For example, adding a column is supported, but removing or changing a column is not supported. If a migration is created to remove or change a column, the ef migrations add
command succeeds but the ef database update
command fails. Due to these limitations, this tutorial doesn't use migrations for SQLite schema changes. Instead, when the schema changes, you drop and re-create the database.
The workaround for the SQLite limitations is to manually write migrations code to perform a table rebuild when something in the table changes. A table rebuild involves:
Creating a new table.
Copying data from the old table to the new table.
Dropping the old table.
Renaming the new table.
For more information, see the following resources:
SQLite EF Core Database Provider Limitations
Customize migration code
Data seeding
SQLite ALTER TABLE statement
Seed the database
Create a new class named SeedData
in the Models folder. Replace the generated code with the following:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using System;
using System.Linq;
namespace MvcMovie.Models;
public static class SeedData
public static void Initialize(IServiceProvider serviceProvider)
using (var context = new MvcMovieContext(
serviceProvider.GetRequiredService<
DbContextOptions<MvcMovieContext>>()))
// Look for any movies.
if (context.Movie.Any())
return; // DB has been seeded
context.Movie.AddRange(
new Movie
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M
new Movie
Title = "Ghostbusters ",
ReleaseDate = DateTime.Parse("1984-3-13"),
Genre = "Comedy",
Price = 8.99M
new Movie
Title = "Ghostbusters 2",
ReleaseDate = DateTime.Parse("1986-2-23"),
Genre = "Comedy",
Price = 9.99M
new Movie
Title = "Rio Bravo",
ReleaseDate = DateTime.Parse("1959-4-15"),
Genre = "Western",
Price = 3.99M
context.SaveChanges();
If there are any movies in the database, the seed initializer returns and no movies are added.
if (context.Movie.Any())
return; // DB has been seeded.
<a name=snippet_"si">
Add the seed initializer
Visual Studio
Visual Studio Code / Visual Studio for Mac
Replace the contents of Program.cs
with the following code. The new code is highlighted.
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using MvcMovie.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext")));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
var services = scope.ServiceProvider;
SeedData.Initialize(services);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Delete all the records in the database. You can do this with the delete links in the browser or from SSOX.
Test the app. Force the app to initialize, calling the code in the Program.cs
file, so the seed method runs. To force initialization, close the command prompt window that Visual Studio opened, and restart by pressing Ctrl+F5.
Update Program.cs
with the following highlighted code:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using MvcMovie.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("MvcMovieContext")));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
var services = scope.ServiceProvider;
SeedData.Initialize(services);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Delete all the records in the database.
Test the app. Stop it and restart it so the SeedData.Initialize
method runs and seeds the database.
The app shows the seeded data.
Previous: Adding a model
Next: Adding controller methods and views
The MvcMovieContext
object handles the task of connecting to the database and mapping Movie
objects to database records. The database context is registered with the Dependency Injection container in the Program.cs
file:
Visual Studio
Visual Studio Code / Visual Studio for Mac
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext")));
The ASP.NET Core Configuration system reads the ConnectionString
key. For local development, it gets the connection string from the appsettings.json
file:
"ConnectionStrings": {
"MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-7dc5;Trusted_Connection=True;MultipleActiveResultSets=true"
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("MvcMovieContext")));
The ASP.NET Core Configuration system reads the ConnectionString
. For local development, it gets the connection string from the appsettings.json
file:
"ConnectionStrings": {
"MvcMovieContext": "Data Source=MvcMovie.db"
When the app is deployed to a test or production server, an environment variable can be used to set the connection string to a production SQL Server. For more information, see Configuration.
Visual Studio
Visual Studio Code / Visual Studio for Mac
SQL Server Express LocalDB
LocalDB:
Is a lightweight version of the SQL Server Express Database Engine, installed by default with Visual Studio.
Starts on demand by using a connection string.
Is targeted for program development. It runs in user mode, so there's no complex configuration.
By default creates .mdf files in the C:/Users/{user} directory.
The SQLite website states:
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. SQLite is the most used database engine in the world.
There are many third party tools you can download to manage and view a SQLite database. The image below is from DB Browser for SQLite. If you have a favorite SQLite tool, leave a comment on what you like about it.
For this tutorial you use the Entity Framework Core migrations feature where possible. Migrations updates the database schema to match changes in the data model. However, migrations can only do the kinds of changes that the EF Core provider supports, and the SQLite provider's capabilities are limited. For example, adding a column is supported, but removing or changing a column is not supported. If a migration is created to remove or change a column, the ef migrations add
command succeeds but the ef database update
command fails. Due to these limitations, this tutorial doesn't use migrations for SQLite schema changes. Instead, when the schema changes, you drop and re-create the database.
The workaround for the SQLite limitations is to manually write migrations code to perform a table rebuild when something in the table changes. A table rebuild involves:
Creating a new table.
Copying data from the old table to the new table.
Dropping the old table.
Renaming the new table.
For more information, see the following resources:
SQLite EF Core Database Provider Limitations
Customize migration code
Data seeding
SQLite ALTER TABLE statement
Seed the database
Create a new class named SeedData
in the Models folder. Replace the generated code with the following:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using System;
using System.Linq;
namespace MvcMovie.Models
public static class SeedData
public static void Initialize(IServiceProvider serviceProvider)
using (var context = new MvcMovieContext(
serviceProvider.GetRequiredService<
DbContextOptions<MvcMovieContext>>()))
// Look for any movies.
if (context.Movie.Any())
return; // DB has been seeded
context.Movie.AddRange(
new Movie
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M
new Movie
Title = "Ghostbusters ",
ReleaseDate = DateTime.Parse("1984-3-13"),
Genre = "Comedy",
Price = 8.99M
new Movie
Title = "Ghostbusters 2",
ReleaseDate = DateTime.Parse("1986-2-23"),
Genre = "Comedy",
Price = 9.99M
new Movie
Title = "Rio Bravo",
ReleaseDate = DateTime.Parse("1959-4-15"),
Genre = "Western",
Price = 3.99M
context.SaveChanges();
If there are any movies in the database, the seed initializer returns and no movies are added.
if (context.Movie.Any())
return; // DB has been seeded.
Add the seed initializer
Visual Studio
Visual Studio Code / Visual Studio for Mac
Replace the contents of Program.cs
with the following code. The new code is highlighted.
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using MvcMovie.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext")));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
var services = scope.ServiceProvider;
SeedData.Initialize(services);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Delete all the records in the database. You can do this with the delete links in the browser or from SSOX.
Test the app. Force the app to initialize, calling the code in the Program.cs
file, so the seed method runs. To force initialization, close the command prompt window that Visual Studio opened, and restart by pressing Ctrl+F5.
Update Program.cs
with the following highlighted code:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using MvcMovie.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("MvcMovieContext")));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
var services = scope.ServiceProvider;
SeedData.Initialize(services);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Delete all the records in the database.
Test the app. Stop it and restart it so the SeedData.Initialize
method runs and seeds the database.
The app shows the seeded data.
Previous: Adding a model
Next: Adding controller methods and views
The MvcMovieContext
object handles the task of connecting to the database and mapping Movie
objects to database records. The database context is registered with the Dependency Injection container in the ConfigureServices
method in the Startup.cs
file:
Visual Studio
Visual Studio Code / Visual Studio for Mac
public void ConfigureServices(IServiceCollection services)
services.AddControllersWithViews();
services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MvcMovieContext")));
The ASP.NET Core Configuration system reads the ConnectionString
key. For local development, it gets the connection string from the appsettings.json
file:
"ConnectionStrings": {
"MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-2;Trusted_Connection=True;MultipleActiveResultSets=true"
public void ConfigureServices(IServiceCollection services)
services.AddControllersWithViews();
services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("MvcMovieContext")));
The ASP.NET Core Configuration system reads the ConnectionString
. For local development, it gets the connection string from the appsettings.json
file:
"ConnectionStrings": {
"MvcMovieContext": "Data Source=MvcMovie.db"
When the app is deployed to a test or production server, an environment variable can be used to set the connection string to a production SQL Server. For more information, see Configuration.
Visual Studio
Visual Studio Code / Visual Studio for Mac
SQL Server Express LocalDB
LocalDB:
Is a lightweight version of the SQL Server Express Database Engine, installed by default with Visual Studio.
Starts on demand by using a connection string.
Is targeted for program development. It runs in user mode, so there's no complex configuration.
By default creates .mdf files in the C:/Users/{user} directory.
Examine the database
From the View menu, open SQL Server Object Explorer (SSOX).
Right-click on the Movie
table > View Designer
Note the key icon next to ID
. By default, EF makes a property named ID
the primary key.
Right-click on the Movie
table > View Data
The SQLite website states:
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. SQLite is the most used database engine in the world.
There are many third party tools you can download to manage and view a SQLite database. The image below is from DB Browser for SQLite. If you have a favorite SQLite tool, leave a comment on what you like about it.
For this tutorial you use the Entity Framework Core migrations feature where possible. Migrations updates the database schema to match changes in the data model. However, migrations can only do the kinds of changes that the EF Core provider supports, and the SQLite provider's capabilities are limited. For example, adding a column is supported, but removing or changing a column is not supported. If a migration is created to remove or change a column, the ef migrations add
command succeeds but the ef database update
command fails. Due to these limitations, this tutorial doesn't use migrations for SQLite schema changes. Instead, when the schema changes, you drop and re-create the database.
The workaround for the SQLite limitations is to manually write migrations code to perform a table rebuild when something in the table changes. A table rebuild involves:
Creating a new table.
Copying data from the old table to the new table.
Dropping the old table.
Renaming the new table.
For more information, see the following resources:
SQLite EF Core Database Provider Limitations
Customize migration code
Data seeding
SQLite ALTER TABLE statement
Seed the database
Create a new class named SeedData
in the Models folder. Replace the generated code with the following:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MvcMovie.Data;
using System;
using System.Linq;
namespace MvcMovie.Models
public static class SeedData
public static void Initialize(IServiceProvider serviceProvider)
using (var context = new MvcMovieContext(
serviceProvider.GetRequiredService<
DbContextOptions<MvcMovieContext>>()))
// Look for any movies.
if (context.Movie.Any())
return; // DB has been seeded
context.Movie.AddRange(
new Movie
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M
new Movie
Title = "Ghostbusters ",
ReleaseDate = DateTime.Parse("1984-3-13"),
Genre = "Comedy",
Price = 8.99M
new Movie
Title = "Ghostbusters 2",
ReleaseDate = DateTime.Parse("1986-2-23"),
Genre = "Comedy",
Price = 9.99M
new Movie
Title = "Rio Bravo",
ReleaseDate = DateTime.Parse("1959-4-15"),
Genre = "Western",
Price = 3.99M
context.SaveChanges();
If there are any movies in the database, the seed initializer returns and no movies are added.
if (context.Movie.Any())
return; // DB has been seeded.
Add the seed initializer
Replace the contents of Program.cs
with the following code:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MvcMovie.Data;
using MvcMovie.Models;
using System;
namespace MvcMovie
public class Program
public static void Main(string[] args)
var host = CreateHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
var services = scope.ServiceProvider;
SeedData.Initialize(services);
catch (Exception ex)
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
host.Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
webBuilder.UseStartup<Startup>();
Test the app.
Visual Studio
Visual Studio Code / Visual Studio for Mac
Delete all the records in the database. You can do this with the delete links in the browser or from SSOX.
Force the app to initialize, calling the methods in the Startup
class, so the seed method runs. To force initialization, IIS Express must be stopped and restarted. You can do this with any of the following approaches:
Right-click the IIS Express system tray icon in the notification area and tap Exit or Stop Site:
If you were running VS in non-debug mode, press F5 to run in debug mode
If you were running VS in debug mode, stop the debugger and press F5
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback.
Submit and view feedback for
This product