From cb616ab7718b17c2a852cd878b96813ce2e1e9f5 Mon Sep 17 00:00:00 2001 From: littlefoot Date: Wed, 13 Oct 2021 11:04:53 -0400 Subject: [PATCH] Added some EFCore stuff to webapi --- WebAPI/Data/AppDbContext.cs | 21 +++++++++++++++++++++ WebAPI/Data/Models/BaseEntity.cs | 12 ++++++++++++ WebAPI/Data/Models/User.cs | 7 +++++++ WebAPI/Startup.cs | 3 +++ WebAPI/WebAPI.csproj | 10 +++++++++- WebAPI/appsettings.json | 3 +++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 WebAPI/Data/AppDbContext.cs create mode 100644 WebAPI/Data/Models/BaseEntity.cs create mode 100644 WebAPI/Data/Models/User.cs diff --git a/WebAPI/Data/AppDbContext.cs b/WebAPI/Data/AppDbContext.cs new file mode 100644 index 0000000..c778e42 --- /dev/null +++ b/WebAPI/Data/AppDbContext.cs @@ -0,0 +1,21 @@ +using System; +using Microsoft.EntityFrameworkCore; +using WebAPI.Data.Models; + +namespace WebAPI.Data +{ + public class AppDbContext : DbContext + { + public DbSet Users { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // Place any config overrides here + base.OnConfiguring(optionsBuilder); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + } + } +} \ No newline at end of file diff --git a/WebAPI/Data/Models/BaseEntity.cs b/WebAPI/Data/Models/BaseEntity.cs new file mode 100644 index 0000000..8777fac --- /dev/null +++ b/WebAPI/Data/Models/BaseEntity.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace WebAPI.Data.Models +{ + public class BaseEntity + { + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [Key] + public int Id { get; set; } + } +} \ No newline at end of file diff --git a/WebAPI/Data/Models/User.cs b/WebAPI/Data/Models/User.cs new file mode 100644 index 0000000..e069de3 --- /dev/null +++ b/WebAPI/Data/Models/User.cs @@ -0,0 +1,7 @@ +namespace WebAPI.Data.Models +{ + public class User : BaseEntity + { + public int PterodactylUserId { get; set; } + } +} \ No newline at end of file diff --git a/WebAPI/Startup.cs b/WebAPI/Startup.cs index 6b4e8bd..5ddd238 100644 --- a/WebAPI/Startup.cs +++ b/WebAPI/Startup.cs @@ -6,11 +6,13 @@ 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 Microsoft.OpenApi.Models; +using WebAPI.Data; namespace WebAPI { @@ -28,6 +30,7 @@ namespace WebAPI { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "WebAPI", Version = "v1"}); }); + services.AddDbContext(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"))); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/WebAPI/WebAPI.csproj b/WebAPI/WebAPI.csproj index 11984f2..40ce389 100644 --- a/WebAPI/WebAPI.csproj +++ b/WebAPI/WebAPI.csproj @@ -5,7 +5,15 @@ - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/WebAPI/appsettings.json b/WebAPI/appsettings.json index d9d9a9b..942e089 100644 --- a/WebAPI/appsettings.json +++ b/WebAPI/appsettings.json @@ -1,4 +1,7 @@ { + "ConnectionStrings": { + "DefaultConnection": "" + }, "Logging": { "LogLevel": { "Default": "Information",