Adds Database Structure in C# with Entity Framework

This commit is contained in:
Henry Trumme
2025-04-22 00:59:47 +02:00
parent 370edc17e9
commit 1e6c7a47a4
71 changed files with 6197 additions and 140 deletions

View File

@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace WatchLog.Data
{
public class Admin
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(100)]
public required string Name { get; set; }
[Required]
public required string Password { get; set; } // Important: Save as HASH
}
}

View File

@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;
namespace WatchLog.Data
{
public class Genre
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public required string Name { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Navigation Properties ---
public virtual ICollection<LinkTableGlobalGenre> LinkTableGlobalGenres { get; set; } = new List<LinkTableGlobalGenre>();
}
}

View File

@@ -0,0 +1,45 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WatchLog.Data
{
public class GlobalEntity
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(200)]
public required string Title { get; set; }
[MaxLength(500)]
public string? PicturePath { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Foreign Keys ---
[Required]
public int TypeId { get; set; }
[Required]
public int CreatorId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(TypeId))]
public virtual Type Type { get; set; } = null!;
[ForeignKey(nameof(CreatorId))]
public virtual User User { get; set; } = null!;
public virtual ICollection<LinkTableGlobalGenre> LinkTableGlobalGenres { get; set; } = new List<LinkTableGlobalGenre>();
public virtual ICollection<PrivateEntity> PrivateEntities { get; set; } = new List<PrivateEntity>();
public virtual ICollection<SharedListEntity> SharedListEntities { get; set; } = new List<SharedListEntity>();
}
}

View File

@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
[PrimaryKey(nameof(GlobalEntityId), nameof(GenreId))]
public class LinkTableGlobalGenre
{
// --- Foreign Keys ---
[Column(Order = 0)]
public int GlobalEntityId { get; set; }
[Column(Order = 1)]
public int GenreId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(GlobalEntityId))]
public virtual GlobalEntity GlobalEntity { get; set; } = null!;
[ForeignKey(nameof(GenreId))]
public virtual Genre Genre { get; set; } = null!;
}
}

View File

@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace WatchLog.Data
{
public class StreamingPlatform
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(100)]
public required string Name { get; set; }
[Required]
public string? PicturePath { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace WatchLog.Data
{
public class Type
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public required string Name { get; set; }
// --- Navigation Property ---
public virtual ICollection<GlobalEntity> GlobalEntities { get; set; } = new List<GlobalEntity>();
}
}

View File

@@ -0,0 +1,32 @@
using System.ComponentModel.DataAnnotations;
namespace WatchLog.Data
{
public class User
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(100)]
public required string Name { get; set; }
[MaxLength(255)]
public string? Email { get; set; }
[Required]
public required string Password { get; set; } // Important: Save as HASH
// --- Navigation Properties ---
public virtual ICollection<PrivateEntity> PrivateEntities { get; set; } = new List<PrivateEntity>();
public virtual ICollection<GlobalEntity> GlobalEntities { get; set; } = new List<GlobalEntity>();
public virtual ICollection<Label> Labels { get; set; } = new List<Label>();
public virtual ICollection<UserWatchStatus> UserWatchStatuses { get; set; } = new List<UserWatchStatus>();
public virtual ICollection<LinkTableSharedUser> LinkTableSharedUsers { get; set; } = new List<LinkTableSharedUser>();
}
}

View File

@@ -0,0 +1,32 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WatchLog.Data
{
public class Label
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public required string Name { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Foreign Key ---
[Required]
public int CreatorId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(CreatorId))]
public virtual User User { get; set; } = null!;
public virtual ICollection<LinkTablePrivateLabel> LinkTablePrivateLabels { get; set; } = new List<LinkTablePrivateLabel>();
}
}

View File

@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
[PrimaryKey(nameof(PrivateEntityId), nameof(LabelId))]
public class LinkTablePrivateLabel
{
// --- Foreign Keys ---
public int PrivateEntityId { get; set; }
public int LabelId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(PrivateEntityId))]
public virtual PrivateEntity PrivateEntity { get; set; } = null!;
[ForeignKey(nameof(LabelId))]
public virtual Label Label { get; set; } = null!;
}
}

View File

@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
[PrimaryKey(nameof(PrivateEntityId), nameof(StreamingPlatformId))]
public class LinkTablePrivateStreamingPlatform
{
// --- Foreign Keys ---
public int PrivateEntityId { get; set; }
public int StreamingPlatformId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(PrivateEntityId))]
public virtual PrivateEntity PrivateEntity { get; set; } = null!;
[ForeignKey(nameof(StreamingPlatformId))]
public virtual StreamingPlatform StreamingPlatform { get; set; } = null!;
}
}

View File

@@ -0,0 +1,52 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WatchLog.Data
{
public class PrivateEntity
{
[Key]
public int Id { get; set; }
public bool Favorite { get; set; } = false;
[MaxLength(1000)]
public string? Description { get; set; }
public int? Season { get; set; }
public int? Episode { get; set; }
public int? Rating { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Foreign Keys ---
[Required]
public int UserId { get; set; }
[Required]
public int GlobalEntityId { get; set; }
public int? UserWatchStatusId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(UserId))]
public virtual User User { get; set; } = null!;
[ForeignKey(nameof(GlobalEntityId))]
public virtual GlobalEntity GlobalEntity { get; set; } = null!;
[ForeignKey(nameof(UserWatchStatusId))]
public virtual UserWatchStatus? UserWatchStatus { get; set; }
public virtual ICollection<LinkTablePrivateLabel> PrivateEntityLabels { get; set; } = new List<LinkTablePrivateLabel>();
public virtual ICollection<LinkTablePrivateStreamingPlatform> PrivateStreamingPlatforms { get; set; } = new List<LinkTablePrivateStreamingPlatform>();
}
}

View File

@@ -0,0 +1,38 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WatchLog.Data
{
public class UserWatchStatus
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public required string Name { get; set; }
[MaxLength(255)]
public string? Description { get; set; }
[MaxLength(7)]
public string? ColorCode { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Foreign Key ---
[Required]
public int UserId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(UserId))]
public virtual User User { get; set; } = null!;
public virtual ICollection<PrivateEntity> PrivateEntities { get; set; } = new List<PrivateEntity>();
}
}

View File

@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
[PrimaryKey(nameof(SharedListLabelId), nameof(SharedListEntityId))]
public class LinkTableSharedLabel
{
// --- Foreign Keys ---
public int SharedListLabelId { get; set; }
public int SharedListEntityId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(SharedListLabelId))]
public virtual SharedListLabel SharedListLabel { get; set; } = null!;
[ForeignKey(nameof(SharedListEntityId))]
public virtual SharedListEntity SharedListEntity { get; set; } = null!;
}
}

View File

@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
[PrimaryKey(nameof(SharedListEnityId), nameof(StreamingPlatformId))]
public class LinkTableSharedStreamingPlatform
{
// --- Foreign Keys ---
public int SharedListEnityId { get; set; }
public int StreamingPlatformId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(SharedListEnityId))]
public virtual SharedListEntity SharedListEntity { get; set; } = null!;
[ForeignKey(nameof(StreamingPlatformId))]
public virtual StreamingPlatform StreamingPlatform { get; set; } = null!;
}
}

View File

@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
[PrimaryKey(nameof(SharedListId), nameof(UserId))]
public class LinkTableSharedUser
{
// --- Foreign Keys ---
public int SharedListId { get; set; }
public int UserId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(SharedListId))]
public virtual SharedList SharedList { get; set; } = null!;
[ForeignKey(nameof(UserId))]
public virtual User User { get; set; } = null!;
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
namespace WatchLog.Data
{
public class SharedList
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(150)]
public required string Name { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Navigation Properties ---
public virtual ICollection<LinkTableSharedUser> SharedListUsers { get; set; } = new List<LinkTableSharedUser>();
public virtual ICollection<SharedListEntity> SharedListEntities { get; set; } = new List<SharedListEntity>();
public virtual ICollection<SharedWatchStatus> SharedWatchStatuses { get; set; } = new List<SharedWatchStatus>();
public virtual ICollection<SharedListLabel> SharedListLabels { get; set; } = new List<SharedListLabel>();
}
}

View File

@@ -0,0 +1,48 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
[PrimaryKey(nameof(SharedListId), nameof(GlobalEntityId))]
public class SharedListEntity
{
// --- Foreign Keys ---
public int SharedListId { get; set; }
public int GlobalEntityId { get; set; }
// --- Weiterer Foreign Key ---
public int? SharedWatchStatusId { get; set; }
// --- Datenfelder für den Listeneintrag ---
public bool Favorite { get; set; } = false;
[MaxLength(1000)]
public string? Description { get; set; }
public int? Season { get; set; }
public int? Episode { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(SharedListId))]
public virtual SharedList SharedList { get; set; } = null!;
[ForeignKey(nameof(GlobalEntityId))]
public virtual GlobalEntity GlobalEntity { get; set; } = null!;
[ForeignKey(nameof(SharedWatchStatusId))]
public virtual SharedWatchStatus? SharedWatchStatus { get; set; }
public virtual ICollection<LinkTableSharedLabel> LinkTableSharedLabels { get; set; } = new List<LinkTableSharedLabel>();
public virtual ICollection<LinkTablePrivateStreamingPlatform> LinkTablePrivateStreamingPlatforms { get; set; } = new List<LinkTablePrivateStreamingPlatform>();
}
}

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WatchLog.Data
{
public class SharedListLabel
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public required string Name { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Foreign Key ---
[Required]
public int SharedListId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(SharedListId))]
public virtual SharedList SharedList { get; set; } = null!;
public virtual ICollection<LinkTableSharedLabel> LinkTableSharedLabels { get; set; } = new List<LinkTableSharedLabel>();
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WatchLog.Data
{
public class SharedWatchStatus
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public required string Name { get; set; }
[MaxLength(255)]
public string? Description { get; set; }
[MaxLength(7)]
public string? ColorCode { get; set; }
[Required]
public required DateTime CreationTime { get; set; }
public DateTime? LastChange { get; set; }
// --- Foreign Key ---
[Required]
public int SharedListId { get; set; }
// --- Navigation Properties ---
[ForeignKey(nameof(SharedListId))]
public virtual SharedList SharedList { get; set; } = null!;
public virtual ICollection<SharedListEntity> SharedListEntities { get; set; } = new List<SharedListEntity>();
}
}

View File

@@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore;
namespace WatchLog.Data
{
public class WatchLogDataContext : DbContext
{
protected readonly IConfiguration Configuration;
public WatchLogDataContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(Configuration.GetConnectionString("WatchLogDB"));
}
// Global
public DbSet<Admin> Admins { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<GlobalEntity> GlobalEntities { get; set; }
//public DbSet<LinkTableGlobalGenre> LinkTableGlobalGenres { get; set; }
public DbSet<StreamingPlatform> StreamingPlatforms { get; set; }
public DbSet<Type> Types { get; set; } // 'Watchlog.Data.Type' if namecolsion with System.Type
public DbSet<User> Users { get; set; }
//Private
public DbSet<Label> Labels { get; set; }
//public DbSet<LinkTablePrivateLabel> LinkTablePrivateLabels { get; set; }
//public DbSet<LinkTablePrivateStreamingPlatform> LinkTablePrivateStreamingPlatform { get; set; }
public DbSet<PrivateEntity> PrivateEntities { get; set; }
public DbSet<UserWatchStatus> UserWatchStatuses { get; set; }
//Shared
//public DbSet<LinkTableSharedLabel> LinkTableSharedLabels { get; set; }
//public DbSet<LinkTableSharedStreamingPlatform> LinkTableSharedStreamingPlatforms { get; set; }
//public DbSet<LinkTableSharedUser> LinkTableSharedUsers { get; set; }
public DbSet<SharedList> SharedLists { get; set; }
public DbSet<SharedListEntity> SharedListEntities { get; set; }
public DbSet<SharedListLabel> SharedListLabels { get; set; }
public DbSet<SharedWatchStatus> SharedWatchStatuses { get; set; }
}
}