Skip to content

Instantly share code, notes, and snippets.

@rowanmiller
rowanmiller / Demo.cs
Last active August 27, 2017 23:04
EF7 | Pluralizing table names
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s");
}
}
@rowanmiller
rowanmiller / Demo.cs
Last active November 5, 2019 09:12
EF6.x | Correlating SQL to code
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace Demo
@rowanmiller
rowanmiller / Demo.cs
Created February 11, 2016 22:31
EF7 | Use DbSet property names as table names
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var dbSets = GetType().GetProperties()
.Where(p => p.PropertyType.Name == "DbSet`1")
.Select(p => new
{
PropertyName = p.Name,
EntityType = p.PropertyType.GenericTypeArguments.Single()
})
.ToArray();
@rowanmiller
rowanmiller / Demo.cs
Last active February 29, 2016 23:58
EF6.x | Same context instance to different databases
using System;
using System.Data.Entity;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
private static string connection_one = "Server=(localdb)\\mssqllocaldb;Database=Blogging_One;Trusted_Connection=True;";
private static string connection_two = "Server=(localdb)\\mssqllocaldb;Database=BLogging_Two;Trusted_Connection=True;";
@rowanmiller
rowanmiller / Demo.cs
Last active July 19, 2016 20:58
EF Core | Custom Model Validation Rules
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
@rowanmiller
rowanmiller / Lab1.cs
Last active April 20, 2017 19:17
Sample Code
using System;
namespace ConsoleApplication2
{
public class Program
{
public static void Main()
{
int mark = 66;
@rowanmiller
rowanmiller / Temp_SampleCode.cs
Created May 19, 2017 20:55
Temp_SampleCode
var names = new List<String>
{
"Ana",
"Felipe",
"Emillia"
};
names.ForEach(name =>
Console.WriteLine($"Hello {name}"));
@rowanmiller
rowanmiller / UrlRedirectTracer.cs
Last active June 11, 2019 22:42
Trace the chain of redirects for a set of URLs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Xml.Linq;
namespace RedirectTracer
{
class Program