added dbcontext
This commit is contained in:
parent
7eefb4e190
commit
3234c68962
|
|
@ -6,5 +6,6 @@ obj
|
|||
.vscode
|
||||
.DS_Store
|
||||
*.db
|
||||
*appsettings.JSON
|
||||
wwwroot
|
||||
publish
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TestApplication.Data;
|
||||
using TestApplication.Models;
|
||||
|
||||
namespace TestApplication.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ValuesController : ControllerBase
|
||||
{
|
||||
private readonly Context _context;
|
||||
public ValuesController(Context context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<Values>>> GetValues()
|
||||
{
|
||||
return await _context.Values.ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using TestApplication.Models;
|
||||
|
||||
namespace TestApplication.Data
|
||||
{
|
||||
public class Context : DbContext
|
||||
{
|
||||
public Context(DbContextOptions<Context> options) : base(options) {}
|
||||
public DbSet<Values> Values {get; set;}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TestApplication.Models
|
||||
{
|
||||
public class Values
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
13
Startup.cs
13
Startup.cs
|
|
@ -1,15 +1,10 @@
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using TestApplication.Data;
|
||||
|
||||
namespace TestApplication
|
||||
{
|
||||
|
|
@ -26,6 +21,10 @@ namespace TestApplication
|
|||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers();
|
||||
services.AddDbContext<Context>(opt =>
|
||||
{
|
||||
opt.UseMySql(Configuration.GetConnectionString("DefaultConnection"));
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.12"/>
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Loading…
Reference in New Issue