I am on an Intel chip Mac.
When I do dotnet run
, I expected the table Player
to be created, and the data to be seeded from ApplicationDbContext.cs
. I see the table being created, but no data inserted.
My implementation is quite basic with an SQLite db, but not sure why it is not working?
Output:
Player.cs
namespace OneModel.Models
{
public class Player
{
public int PlayerId { get; set; }
public string PlayerName { get; set; }
public Player()
{
}
}
}
ApplicationDbContext.cs
using Microsoft.EntityFrameworkCore;
using OneModel.Models;
namespace OneModel.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Player> Players { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Player>().HasData(
new
{
PlayerId = 1,
PlayerName = "Edgar",
}
);
}
}
}
Program.cs
using Microsoft.EntityFrameworkCore;
using OneModel.Data;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(connectionString));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// 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?}");
using (var scope = app.Services.CreateScope()) {
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
}
app.Run();
new{PlayerName = "Edgar"}
instead ofnew{PlayerId = 1,PlayerName = "Edgar"}
? – Natie