Facebook
From Hot Hummingbird, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 111
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Microsoft.EntityFrameworkCore;
  4. using Utils;
  5. using System.Linq;
  6. using Microsoft.EntityFrameworkCore.Design;
  7.  
  8. namespace App
  9. {
  10.     [ExcludeFromCodeCoverage]
  11.     public class ProjectContextFactory : IDesignTimeDbContextFactory<Context>
  12.     {
  13.         public Context CreateDbContext(string[] args)
  14.         {
  15.             var optionsBuilder = new DbContextOptionsBuilder<Context>();
  16.             optionsBuilder.UseSqlite("Data Source=project.db",
  17.                 b => b.MigrationsAssembly("App"));
  18.  
  19.             return new Context(optionsBuilder.Options);
  20.         }
  21.     }
  22.    
  23.     [ExcludeFromCodeCoverage]
  24.     internal static class Program
  25.     {
  26.         private static void Main()
  27.         {
  28.             var ctx = new ProjectContextFactory();
  29.            
  30.             using (var db = ctx.CreateDbContext(Array.Empty<string>()))
  31.             {
  32.                 var project = new Project
  33.                 {
  34.                     Name = "Dummy Name",
  35.                     Description = "Dummy Description",
  36.                     CreationDate = DateTime.Today,
  37.                     Uri = new Uri("http://www.example.com")
  38.                 };
  39.                
  40.                 db.Projects?.Add(project);
  41.                 db.SaveChanges();
  42.             }
  43.            
  44.             using (var db = ctx.CreateDbContext(Array.Empty<string>()))
  45.             {
  46.                 var projects = db.Projects?
  47.                     .Where(b => b.Name == "Dummy Name")
  48.                     .OrderBy(b => b.Description)
  49.                     .ToList();
  50.  
  51.                 foreach (var project in projects!)
  52.                 {
  53.                     Console.WriteLine($"{project.Id} " +
  54.                                       $"{project.Name} " +
  55.                                       $"{project.Description} " +
  56.                                       $"{project.CreationDate} " +
  57.                                       $"{project.Uri}");
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }