Data Model 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public class User { [Key] public Guid Id { get; set; } [Required] public string UserName { get; set; } = "Giga Chad"; public byte[] Image { get; set; }= DefaultImage(); private static byte[] DefaultImage() { // Provide logic to load a default image from a file or resource // For example: // return File.ReadAllBytes("default_image.png"); return new byte[0]; // Placeholder default image } public List<Challenge> Challenges { get; set; } } public class Challenge { [Key] public Guid Id { get; set; } [Required] public string Name { get; set; } = "Challenge"; [Required] public int DayCount { get; set; } [Required] public DateTime StartDate { get; set; } [Required] public DateTime EndDate { get; set; } public List<ToDo> ToDos { get; set; } [Required] public Guid UserId { get; set; } // Foreign key public User User { get; set; } // Navigation property } public class ToDo { [Key] public Guid Id { get; set; } public bool IsCompleted { get; set; } [Required] public string Description { get; set; } = "Exercise"; public DateTime CompletionDate { get; set; } public Guid ChallengeId { get; set; } // Foreign key public Challenge Challenge { get; set; } // Navigation property } DBContext or Repositiory 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 public class Repository : DbContext { public Repository(DbContextOptions<Repository> opts) : base(opts) { } public DbSet<User> Users { get; set; } public DbSet<Challenge> Challenges { get; set; } public DbSet<ToDo> ToDos { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { #region Seed Users var user = new User { Id = Guid.NewGuid(), UserName = "Default", Image = GetDefaultImage(), }; var chlng = new Challenge { Id = Guid.NewGuid(), Name = "Streak Visitors", DayCount = 7, StartDate = DateTime.Now, UserId = user.Id }; var todoOne = new ToDo { Id = Guid.NewGuid(), Description = "Task 1", ChallengeId = chlng.Id }; var todoTwo = new ToDo { Id = Guid.NewGuid(), Description = "Task 2", ChallengeId = chlng.Id }; #endregion modelBuilder.Entity<User>().HasData(user); // Seed Challenges modelBuilder.Entity<Challenge>().HasData(chlng); // Seed ToDos modelBuilder.Entity<ToDo>().HasData(todoOne,todoTwo); base.OnModelCreating(modelBuilder); } private byte[] GetDefaultImage() { // Provide logic to load a default image from a file or resource // For example: // return File.ReadAllBytes("default_image.png"); return new byte[0]; // Placeholder default image } } In Case of Live Design/DbCreation 1 2 3 4 5 6 7 8 9 10 11 12 13 public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<Repository> { public Repository CreateDbContext(string[] args) { var connectionString = "Data Source=database.db"; var optionsBuilder = new DbContextOptionsBuilder<Repository>(); optionsBuilder.UseSqlite(connectionString); return new Repository(optionsBuilder.Options); } } Setting up Connection String and using it
...