53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
public class ChattingContext : DbContext
|
|
{
|
|
public DbSet<Room> Rooms { get; set; }
|
|
|
|
public DbSet<RoomChatMessage> RoomChatMessages { get; set; }
|
|
|
|
public string DbPath { get; }
|
|
|
|
public ChattingContext()
|
|
{
|
|
DbPath = "chat.db";
|
|
}
|
|
|
|
// The following configures EF to create a Sqlite database file in the
|
|
// special "local" folder for your platform.
|
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
|
=> options.UseSqlite($"Data Source={DbPath}");
|
|
}
|
|
|
|
public class Room
|
|
{
|
|
[Key]
|
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
|
public string? Id { get; set; }
|
|
|
|
public string? RoomJoinCode { get; set; }
|
|
|
|
// koble tilsammen RoomChatMessage til Room
|
|
// https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx
|
|
public RoomChatMessage? RoomChatMessage { get; set; }
|
|
}
|
|
|
|
public class RoomChatMessage
|
|
{
|
|
[Key]
|
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
|
public string? Id { get; set; }
|
|
|
|
public string? User { get; set; }
|
|
public string? Message { get; set; }
|
|
public string? RoomId { get; set; }
|
|
|
|
// koble tilsammen RoomChatMessage til Room
|
|
// https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx
|
|
public Room? Room { get; set; }
|
|
} |