C
C#•5mo ago
Carlos David

OnModelCreating dont work in Memory Database

I try create API for list menu.
public class Context : DbContext
{
public Context(DbContextOptions<Context> options)
: base(options)
{
}

public DbSet<Sandwich> Sandwiches { get; set; } = null!;
public DbSet<Extra> Extras { get; set; } = null!;
public DbSet<Order> Orders { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Console.WriteLine("OnModelCreating Executado");
// Gerando a seed de dados
modelBuilder.Entity<Sandwich>().HasData(
new Sandwich { Id = 1, Name = "X Burger", Price = 5.00m },
new Sandwich { Id = 2, Name = "X Egg", Price = 4.50m },
new Sandwich { Id = 3, Name = "X Bacon", Price = 7.00m }
);

modelBuilder.Entity<Extra>().HasData(
new Extra { Id = 1, Name = "Fries", Price = 2.00m },
new Extra { Id = 2, Name = "Soft Drink", Price = 2.50m }
);
}
}
public class Context : DbContext
{
public Context(DbContextOptions<Context> options)
: base(options)
{
}

public DbSet<Sandwich> Sandwiches { get; set; } = null!;
public DbSet<Extra> Extras { get; set; } = null!;
public DbSet<Order> Orders { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Console.WriteLine("OnModelCreating Executado");
// Gerando a seed de dados
modelBuilder.Entity<Sandwich>().HasData(
new Sandwich { Id = 1, Name = "X Burger", Price = 5.00m },
new Sandwich { Id = 2, Name = "X Egg", Price = 4.50m },
new Sandwich { Id = 3, Name = "X Bacon", Price = 7.00m }
);

modelBuilder.Entity<Extra>().HasData(
new Extra { Id = 1, Name = "Fries", Price = 2.00m },
new Extra { Id = 2, Name = "Soft Drink", Price = 2.50m }
);
}
}
When i call endpoing /api/menu i see Console log, but return empty sandwich and extra. Obs: When i use EnsureCreated, workly
No description
7 Replies
Jimmacle
Jimmacle•5mo ago
to clarify, you're saying that when you call EnsureCreated that it works correctly? that is intended behavior, in order to apply changes you've made to your database model you either need to call EnsureCreated or use migrations
Jimmacle
Jimmacle•5mo ago
Data Seeding - EF Core
Using data seeding to populate a database with an initial set of data using Entity Framework Core
Jimmacle
Jimmacle•5mo ago
you should be using migrations if you have any plans to evolve the model over time
Carlos David
Carlos DavidOP•5mo ago
Yes, i tried set directly in controller the método EnsureCreated and i See The console log two times, but return the correctly data of sandwich and extra 😅Ah ok, it's my first time with C# and I didn't know that I have one more question, if you can answer I can set EnsureCreated when i started program?
Jimmacle
Jimmacle•5mo ago
You can but it won't work if the database already exists Migrations are a much better choice for that
Carlos David
Carlos DavidOP•5mo ago
Without a doubt, but I'm doing for a job and they specified that I need to use the in-memory database😭 kkkkk but the article you sent me seems very useful, I will try to implement it, thank you! with help Chat GPT, i did this
var app = builder.Build();

# region Seed de dados
using (var scope = app.Services.CreateScope())
{
// Para banco de dados na memória é necessário fazer na mão utilizando o EnsureCreted
// Fonte: https://learn.microsoft.com/en-us/ef/core/modeling/data-seeding
var context = scope.ServiceProvider.GetRequiredService<Context>();
context.Database.EnsureCreated();
}
# endregion
var app = builder.Build();

# region Seed de dados
using (var scope = app.Services.CreateScope())
{
// Para banco de dados na memória é necessário fazer na mão utilizando o EnsureCreted
// Fonte: https://learn.microsoft.com/en-us/ef/core/modeling/data-seeding
var context = scope.ServiceProvider.GetRequiredService<Context>();
context.Database.EnsureCreated();
}
# endregion
Console log show 2 times, but dont duplicate data. I ignore because its working
jcotton42
jcotton42•5mo ago
Why do they want you to use in-memory? EF’s in-memory database is a trap.
Want results from more Discord servers?
Add your server