V
V
VanilaSpirit2020-06-12 19:51:03
PostgreSQL
VanilaSpirit, 2020-06-12 19:51:03

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){}
}


In startup:

string connection = Configuration.GetConnectionString("DefaultConnection");
    services.AddDbContext<ProductContext>(options =>
        options.UseNpgsql(connection));


Here's what I'm trying to get at:

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);
        }


In response to the request comes [] - empty JSON
The connection string to the database works, in any case, if you specify an incorrect password / name, it drops the program. I define it like this

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-06-13
@vabka

More like the database is empty

V
Vladimir Korotenko, 2020-06-13
@firedragon

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 question

Ask a Question

731 491 924 answers to any question