added dbcontext

This commit is contained in:
Ashraful Haque 2021-02-22 08:00:00 +00:00
parent 7eefb4e190
commit 3234c68962
7 changed files with 60 additions and 21 deletions

1
.gitignore vendored
View File

@ -6,5 +6,6 @@ obj
.vscode
.DS_Store
*.db
*appsettings.JSON
wwwroot
publish

View File

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

11
Data/Context.cs Normal file
View File

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

11
Models/Values.cs Normal file
View File

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

View File

@ -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.

View File

@ -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>

View File

@ -1,10 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}