Hi @Alvin Bernardo ,
You can prepare an in-memory database so that you do not have to use a real SQL server during the test. Install the following 2 packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.InMemory
With this, we can continue to create the database context and model.
Create a new class called Register.cs
public class Register
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Range(40, 60)]
public int Age { get; set; }
}
Then create the Database Context class
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Register> Register { get; set; }
}
The Register.cs class has been added to the properties of the database context. So now you can perform Entity Framework Core operations on it. Next, add the database context as a service in the ConfigureServices() method of the Startup.cs class, as shown below:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(optionsBuilder => optionsBuilder.UseInMemoryDatabase("InMemoryDb"));
services.AddControllersWithViews();
}
If the answer is helpful, please click "Accept Answer" and upvote it.
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,
ChaoDeng