Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using System.Linq;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Logging;
  8.  
  9.  
  10. namespace SampleDb
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             using (var context = new SampleContext())
  17.             {
  18.                 //A query to get all streams from an account and the subscribers during that stream
  19.                 var result =
  20.                     context.Accounts.Where(account => account.Id == 6)
  21. .Join(context.Streams, outer => outer.Id, inner => inner.AccountId,
  22.     (account, stream) => new {Account = account, Stream = stream})
  23. .GroupJoin(context.Subscriptions, outer => outer.Account.Id, inner => inner.TargetAccountId,
  24.     (outer, subscriptions) => new
  25.         {Account = outer.Account, Stream = outer.Stream, Subscriptions = subscriptions})
  26. .SelectMany(x =>
  27.         x.Subscriptions.Where(subscription =>
  28.                 subscription.Date > x.Stream.StreamStart &&
  29.                 subscription.Date < x.Stream.StreamEnd)
  30.             .DefaultIfEmpty(),
  31.     (x, subscription) => new {x.Account, x.Stream, Subscription = subscription}).ToList();
  32.             }
  33.         }
  34.     }
  35.  
  36.     public class SampleContext : DbContext
  37.     {
  38.         public DbSet<Account> Accounts { get; set; }
  39.         public DbSet<Stream> Streams { get; set; }
  40.         public DbSet<Subscription> Subscriptions { get; set; }
  41.  
  42.         protected override void OnModelCreating(ModelBuilder modelBuilder)
  43.         {
  44.             modelBuilder.Entity<Subscription>()
  45.                 .HasKey(bc => new { bc.SourceAccountId, bc.TargetAccountId });
  46.  
  47.             modelBuilder.Entity<Subscription>()
  48.                 .HasOne(bc => bc.SourceAccount)
  49.                 .WithMany(b => b.Subscribers)
  50.                 .HasForeignKey(s => s.SourceAccountId);
  51.  
  52.  
  53.             modelBuilder.Entity<Subscription>()
  54.                 .HasOne(bc => bc.TargetAccount)
  55.                 .WithMany(c => c.Subscriptions)
  56.                 .HasForeignKey(s => s.TargetAccountId);
  57.  
  58.             modelBuilder.Entity<Account>().HasData(
  59.                 new { Id = 1, Name = "SomeViewer1" },
  60.                 new { Id = 2, Name = "SomeViewer2" },
  61.                 new { Id = 3, Name = "SomeViewer3" },
  62.                 new { Id = 4, Name = "SomeViewer4" },
  63.                 new { Id = 5, Name = "SomeViewer5" },
  64.                 new { Id = 6, Name = "SomeStreamer" }
  65.             );
  66.  
  67.             modelBuilder.Entity<Stream>().HasData(
  68.                 new { Id = 1, Title = "my first stream", AccountId = 6,
  69.                     StreamStart = new DateTime(2019,8, 22, 19, 0,0),
  70.                     StreamEnd = new DateTime(2019, 8, 22, 20, 0, 0) },
  71.  
  72.                 new { Id = 2, Title = "my second stream", AccountId = 6,
  73.                     StreamStart = new DateTime(2019, 8, 23, 19, 0, 0),
  74.                     StreamEnd = new DateTime(2019, 8, 23, 20, 0, 0)
  75.                 });
  76.  
  77.             modelBuilder.Entity<Subscription>().HasData(
  78.                 //Not Subscribed during a stream
  79.                 new {SourceAccountId = 1, TargetAccountId = 6, Date = new DateTime(2019, 8, 21, 19, 10, 0) },
  80.  
  81.                 //Subscribed during first stream
  82.                 new {SourceAccountId = 2, TargetAccountId = 6, Date = new DateTime(2019, 8, 22, 19, 15, 0) },
  83.                 new {SourceAccountId = 3, TargetAccountId = 6, Date = new DateTime(2019, 8, 22, 19, 15, 0) },
  84.  
  85.                 //Subscribed during second stream
  86.                 new { SourceAccountId = 4, TargetAccountId = 6, Date = new DateTime(2019, 8, 23, 19, 30, 0) },
  87.                 new {SourceAccountId = 5, TargetAccountId = 6, Date = new DateTime(2019, 8, 23, 19, 13, 0) }
  88.             );
  89.         }
  90.  
  91.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  92.             => optionsBuilder.UseNpgsql("Host=localhost;Database=my_db;Port=2345;Username=postgres;Password=postgres")
  93.                 .UseLoggerFactory(new LoggerFactory().AddConsole())
  94.                 .EnableSensitiveDataLogging();
  95.     }
  96.  
  97.     public class Stream
  98.     {
  99.         [Key]
  100.         public int Id { get; set; }
  101.         public string Title { get; set; }
  102.         [ForeignKey(nameof(Account))]
  103.         public int AccountId { get; set; }
  104.         public DateTime StreamStart { get; set; }
  105.         public DateTime StreamEnd { get; set; }
  106.         public Account Account { get; set; }
  107.     }
  108.  
  109.     public class Subscription
  110.     {
  111.         public int SourceAccountId { get; set; }
  112.         public int TargetAccountId { get; set; }
  113.  
  114.         public Account SourceAccount { get; set; }
  115.         public Account TargetAccount { get; set; }
  116.         public DateTime Date { get; set; }
  117.     }
  118.  
  119.     public class Account
  120.     {
  121.         [Key]
  122.         public int Id { get; set; }
  123.         public string Name { get; set; }
  124.         public ICollection<Subscription> Subscribers { get; set; }
  125.         public ICollection<Subscription> Subscriptions { get; set; }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement