public class DatabaseContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<CodeSnippet> CodeSnippets { get; set; }
public DatabaseContext(DbContextOptions options) : base(options)
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasMany(u => u.CodeSnippets)
.WithOne(c => c.User).HasForeignKey(c => c.UserId);
modelBuilder.Entity<User>().HasMany(u => u.LikedCodeSnippets)
.WithMany(c => c.UsersWhoLiked);
}
}
public class User
{
public int Id { get; set; }
public string? Name { get; set; }
public required string Email { get; set; }
public required string HashPassword { get; set; }
public List<CodeSnippet> CodeSnippets { get; set; } = new List<CodeSnippet>();
public List<CodeSnippet> LikedCodeSnippets { get; set; } = new List<CodeSnippet>();
public IdentityData? IdentityData { get; set; }
}
public class CodeSnippet
{
public int Id { get; set; }
public int UserId { get; set; }
public User User { get; set; } = null!;
public List<User> UsersWhoLiked { get; set; } = new List<User>();
public required string Title { get; set; }
public required string Description { get; set; }
public required string Content { get; set; }
public required string Tag { get; set; }
}
public List<CodeSnippet> LikedCodeSnippets { get; set; } = new List<CodeSnippet>();
public List<User> UsersWhoLiked { get; set; } = new List<User>();
public sealed class UserCodes
{
public int UserId { get; set; }
public User User { get; set; }
public int CodeId { get; set; }
public CodeSnippet Code { get; set; }
}
public User User { get; set; } = null!;
null
и ставить !
как обозначение того, что она not null
?