Answer the question
In order to leave comments, you need to log in
ASP.NET Core MVC how to get data from PostgreSQL?
I'm using EF Core.
Yesterday I connected the database itself, but I don't understand how to get the values from it.
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public int Price { get; set; }
}
public class ProductContext : DbContext
{
public DbSet<Product> Products { get; set; }
public ProductContext(DbContextOptions<ProductContext> options) : base(options){}
}
string connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<ProductContext>(options =>
options.UseNpgsql(connection));
private readonly ProductContext _context;
public Category(ProductContext context)
{
_context = context;
}
[HttpGet]
[Route("category/test")]
public IActionResult test()
{
var result = _context.Products.ToList();
return Ok(result);
}
Answer the question
In order to leave comments, you need to log in
What prevents you from adding unittest and checking both the creation and the presence of data there?
Tests are a good thing
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question