Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6.  
  7. namespace enitty_test
  8. {
  9. class Program
  10. {
  11. public class Planet
  12. {
  13. [Key]
  14. public string Name { get; set; }
  15. public decimal AverageDistanceFromSun { get; set; }
  16. }
  17.  
  18. public class PlanetContext : DbContext
  19. {
  20. public PlanetContext()
  21. {
  22. Database.EnsureCreated();
  23. }
  24.  
  25. public DbSet<Planet> Planets { get; set; }
  26.  
  27. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  28. => optionsBuilder.UseNpgsql("Host=localhost;Database=entity;Username=postgres;Password=qwerty");
  29. }
  30.  
  31. static void Main(string[] args)
  32. {
  33. using (var context = new PlanetContext())
  34. {
  35.  
  36. var jupiter = new Planet
  37. {
  38. Name = "Jupiter",
  39. AverageDistanceFromSun = 775
  40. };
  41. context.Planets.Update(jupiter);
  42. context.SaveChanges();
  43. }
  44.  
  45. using (var context = new PlanetContext())
  46. {
  47. var jupiter = context.Planets.Single(p => p.Name == "Jupiter");
  48. Console.WriteLine($"Jupiter is {jupiter.AverageDistanceFromSun} million km from the sun.");
  49. }
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement