Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- namespace enitty_test
- {
- class Program
- {
- public class Planet
- {
- [Key]
- public string Name { get; set; }
- public decimal AverageDistanceFromSun { get; set; }
- }
- public class PlanetContext : DbContext
- {
- public PlanetContext()
- {
- Database.EnsureCreated();
- }
- public DbSet<Planet> Planets { get; set; }
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- => optionsBuilder.UseNpgsql("Host=localhost;Database=entity;Username=postgres;Password=qwerty");
- }
- static void Main(string[] args)
- {
- using (var context = new PlanetContext())
- {
- var jupiter = new Planet
- {
- Name = "Jupiter",
- AverageDistanceFromSun = 775
- };
- context.Planets.Update(jupiter);
- context.SaveChanges();
- }
- using (var context = new PlanetContext())
- {
- var jupiter = context.Planets.Single(p => p.Name == "Jupiter");
- Console.WriteLine($"Jupiter is {jupiter.AverageDistanceFromSun} million km from the sun.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement