Test EF / Mysql

This commit is contained in:
2018-10-05 00:18:37 +02:00
parent 5a690e4dbe
commit d2c1a9d5f9
7 changed files with 76 additions and 3 deletions

View File

@@ -0,0 +1,20 @@

using System;
using LaDOSE.Entity;
using Microsoft.EntityFrameworkCore;
namespace LaDOSE.Api.Context
{
public class LaDOSEDbContext : DbContext
{
public DbSet<Game> Game { get; set; }
public LaDOSEDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}

View File

@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LaDOSE.Api.Context;
using LaDOSE.Entity;
using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
@@ -10,11 +12,20 @@ namespace LaDOSE.Api.Controllers
[ApiController]
public class ConfigController : ControllerBase
{
private readonly LaDOSEDbContext _db;
public ConfigController(LaDOSEDbContext db)
{
_db = db;
}
// GET api/Config
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
public ActionResult<IEnumerable<Game>> Get()
{
return new string[] { "value1", "value2" };
return _db.Game.ToList();
}
// GET api/Config/5

View File

@@ -10,6 +10,11 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" />
</ItemGroup>
</Project>

View File

@@ -2,14 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LaDOSE.Api.Context;
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.Logging;
using Microsoft.Extensions.Options;
using Pomelo.EntityFrameworkCore.MySql;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
namespace LaDOSE.Api
{
@@ -26,7 +30,16 @@ namespace LaDOSE.Api
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContextPool<LaDOSEDbContext>( // replace "YourDbContext" with the class name of your DbContext
options => options.UseMySql("Server=localhost;Database=ladose;User=root;Password=;", // replace with your Connection String
mysqlOptions =>
{
mysqlOptions.ServerVersion(new Version(10, 1, 16), ServerType.MariaDb); // replace with your Server Version and Type
}
));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)

View File

@@ -0,0 +1,11 @@
using System;
namespace LaDOSE.Entity
{
public class Game
{
public int Id { get; set; }
public string Name { get; set; }
public string ImgUrl { get; set; }
}
}

View File

@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -5,7 +5,9 @@ VisualStudioVersion = 15.0.28010.2041
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LaDOSE.DiscordBot", "LaDOSE.DiscordBot\LaDOSE.DiscordBot.csproj", "{C1E8FAF6-3A75-44AC-938D-CA56A5322D1C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaDOSE.Api", "LaDOSE.Api\LaDOSE.Api.csproj", "{4D7ED4CC-F78F-4860-B8CE-DA01DCACCBB9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LaDOSE.Api", "LaDOSE.Api\LaDOSE.Api.csproj", "{4D7ED4CC-F78F-4860-B8CE-DA01DCACCBB9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaDOSE.Entity", "LaDOSE.Entity\LaDOSE.Entity.csproj", "{B32A4AD5-9A4B-4D12-AAD5-55541F170E2A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
{4D7ED4CC-F78F-4860-B8CE-DA01DCACCBB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D7ED4CC-F78F-4860-B8CE-DA01DCACCBB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D7ED4CC-F78F-4860-B8CE-DA01DCACCBB9}.Release|Any CPU.Build.0 = Release|Any CPU
{B32A4AD5-9A4B-4D12-AAD5-55541F170E2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B32A4AD5-9A4B-4D12-AAD5-55541F170E2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B32A4AD5-9A4B-4D12-AAD5-55541F170E2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B32A4AD5-9A4B-4D12-AAD5-55541F170E2A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE