All models created, before making migrations

This commit is contained in:
Cristiano
2025-10-17 08:36:11 +02:00
parent ffb37cece3
commit 616d2cf34d
4 changed files with 57 additions and 1 deletions

View File

@@ -0,0 +1,44 @@
using BlogPlatform.Models;
namespace BlogPlatform;
using Microsoft.EntityFrameworkCore;
public class BlogPlatformDbContext: DbContext
{
public DbSet<Comment> Comments { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString =
"Server=romaric-thibault.fr;" +
"Database=BlogPlatform_cristiano;" +
"User Id=cristiano;" +
"Password=Onto9-Cage-Afflicted;" +
"TrustServerCertificate=True";
optionsBuilder.UseSqlServer(connectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Comment>()
.Property(comment => comment.CreatedAt)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAdd();
modelBuilder.Entity<Post>()
.Property(post => post.CreatedAt)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAdd();
modelBuilder.Entity<User>()
.Property(user => user.CreatedAt)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAdd();
}
}

View File

@@ -7,4 +7,10 @@ public class Comment
[Key] public int Id { get; set; }
[Required, Length(1,500)] public string? Content { get; set; }
public DateOnly CreatedAt { get; set; }
[Required] public int PostId{ get; set; }
public Post? Post{ get; set; }
[Required] public int UserId { get; set; }
public User? User{ get; set; }
}

View File

@@ -9,4 +9,9 @@ public class Post
[Required] public string? Content { get; set; }
[Required] public int Likes { get; set; }
public DateOnly CreatedAt { get; set; }
public List<Comment>? Comments { get; set; }
[Required] public int UserId { get; set; }
public User? User{ get; set; }
}

View File

@@ -11,5 +11,6 @@ public class User
[Required,Length(24,24)] public string? Salt { get; set; }
public DateOnly CreatedAt { get; set; }
public List<Comment>? Comments { get; set; }
public List<Post>? Posts { get; set; }
}