Skip to content

Instantly share code, notes, and snippets.

@chyld
Last active September 27, 2021 07:40
Show Gist options
  • Save chyld/1cf1f7199169d2086392037c2e4bb42c to your computer and use it in GitHub Desktop.
Save chyld/1cf1f7199169d2086392037c2e4bb42c to your computer and use it in GitHub Desktop.
dotnet new tool-manifest
dotnet tool install dotnet-ef
dotnet ef
dotnet add App/App.csproj package Microsoft.EntityFrameworkCore.Design
dotnet add App/App.csproj package Microsoft.EntityFrameworkCore.Tools
dotnet add App/App.csproj package Microsoft.EntityFrameworkCore.SQLite
dotnet ef migrations add "initial"
dotnet ef migrations list
dotnet ef database update
-----
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace App
{
public class DataContext : DbContext
{
public DbSet<Dog> Dogs { get; set; }
public DbSet<Owner> Owners { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=app.db");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var dogs = new Dog[] {
new Dog { Id = 1, Name = "Lassy"},
new Dog { Id = 2, Name = "Molly"},
new Dog { Id = 3, Name = "Frank"},
};
modelBuilder.Entity<Dog>().HasData(dogs);
var owners = new Owner[] {
new Owner { Id = 1, Name = "Don"},
new Owner { Id = 2, Name = "Jackie"},
new Owner { Id = 3, Name = "Sara"},
};
modelBuilder.Entity<Owner>().HasData(owners);
base.OnModelCreating(modelBuilder);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace App
{
class Program
{
static void Main(string[] args)
{
var database = new DataContext();
Console.WriteLine("#1");
Console.WriteLine("Basic query");
var dogs = database.Dogs.ToList();
foreach(var dog in dogs)
{
Console.WriteLine($"{dog.Id} : {dog.Name}");
}
Console.WriteLine("#2");
Console.WriteLine("Using OrderBy");
database.Dogs.OrderByDescending(d => d.Name).ToList().ForEach(dog => Console.WriteLine(dog.Name));
Console.WriteLine("#3");
Console.WriteLine("Using Where");
database.Dogs.Where(d => d.Id > 1).OrderBy(d => d.Name).ToList().ForEach(dog => Console.WriteLine(dog.Name));
Console.WriteLine("#4");
Console.WriteLine("Inserting a record");
var d1 = new Dog{Name="Zoro"};
database.Add(d1);
Console.WriteLine("#5");
Console.WriteLine("Inserting multiple records");
var puppies = new List<Dog>{new Dog{Name="Frodo"}, new Dog{Name="Gandalf"}};
database.AddRange(puppies);
database.SaveChanges();
Console.WriteLine("#6");
Console.WriteLine("Associating records");
var owner = database.Owners.Where(o => o.Name == "Sara").First();
database.Dogs.Where(d => d.Id > 3).ToList().ForEach(d => d.Owner = owner);
database.SaveChanges();
Console.WriteLine("#7");
Console.WriteLine("Querying a relationship");
var sara = database.Owners.Where(o => o.Name == "Sara").First();
Console.WriteLine($"Owner: {sara.Name}");
sara.Pets.ToList().ForEach(p => Console.WriteLine($"Pet: {p.Name}"));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace App
{
class Program
{
static void Main(string[] args)
{
var database = new DataContext();
Console.WriteLine("#1");
Console.WriteLine("Basic query");
var dogs = database.Dogs.ToList();
foreach(var dog in dogs)
{
Console.WriteLine($"{dog.Id} : {dog.Name}");
}
Console.WriteLine("#2");
Console.WriteLine("Using OrderBy");
database.Dogs.OrderByDescending(d => d.Name).ToList().ForEach(dog => Console.WriteLine(dog.Name));
Console.WriteLine("#3");
Console.WriteLine("Using Where");
database.Dogs.Where(d => d.Id > 1).OrderBy(d => d.Name).ToList().ForEach(dog => Console.WriteLine(dog.Name));
Console.WriteLine("#4");
Console.WriteLine("Inserting a record");
var d1 = new Dog{Name="Zoro", Age=21};
database.Add(d1);
Console.WriteLine("#5");
Console.WriteLine("Inserting multiple records");
var puppies = new List<Dog>{new Dog{Name="Frodo", Age=23}, new Dog{Name="Gandalf", Age=25}};
database.AddRange(puppies);
database.SaveChanges();
Console.WriteLine("#6");
Console.WriteLine("Associating records");
var owner = database.Owners.Where(o => o.Name == "Sara").First();
database.Dogs.Where(d => d.Id > 3).ToList().ForEach(d => d.Owner = owner);
database.SaveChanges();
Console.WriteLine("#7");
Console.WriteLine("Querying a relationship");
var sara = database.Owners.Where(o => o.Name == "Sara").First();
Console.WriteLine($"Owner: {sara.Name}");
sara.Pets.ToList().ForEach(p => Console.WriteLine($"Pet: {p.Name}"));
// ----------------------------------
// using LINQ instead of method calls
// ----------------------------------
Console.WriteLine("\nBasic query");
var dogs1 = from dog in database.Dogs
select dog;
dogs1.ToList().ForEach(dog => Console.WriteLine(dog.Name));
Console.WriteLine("\nGet dogs by name in descending order");
dogs1 = from dog in database.Dogs
orderby dog.Name descending
select dog;
dogs1.ToList().ForEach(dog => Console.WriteLine(dog.Name));
Console.WriteLine("\nGet names of dogs in ascending order");
var names = from dog in database.Dogs
orderby dog.Name ascending
select dog.Name;
names.ToList().ForEach(name => Console.WriteLine(name));
Console.WriteLine("\nCreate anonymous object from query");
var objects1 = from dog in database.Dogs
select new {Name=dog.Name, Owner=dog.Owner.Name ?? "Anonymous"};
objects1.ToList().ForEach(obj => Console.WriteLine($"{obj.Owner} is the owner of {obj.Name}"));
Console.WriteLine("\nUsing where");
var objects2 = from dog in database.Dogs
where dog.Age > 4
select new {Name=dog.Name, Age=dog.Age};
objects2.ToList().ForEach(obj => Console.WriteLine($"{obj.Name} is {obj.Age} years old"));
Console.WriteLine("\nGet the first result from a query");
var object3 = (from dog in database.Dogs
select new {Name=dog.Name, Age=dog.Age}).First();
Console.WriteLine($"The first dog is {object3.Name} and is {object3.Age} years old");
Console.WriteLine("\nGet the count of dogs");
var count = (from dog in database.Dogs
select new {Name=dog.Name, Age=dog.Age}).Count();
Console.WriteLine($"There are {count} dogs in the database");
Console.WriteLine("\nUsing quantifiers");
var owners1 = from o in database.Owners
where o.Pets.Any(pet => pet.Age > 20)
select o;
owners1.ToList().ForEach(owner => Console.WriteLine($"{owner.Name} has at least one dog over the age of 20"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment