Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>LinqBenchmarks.LinqBenchmark-20240119-231018</title>
<style type="text/css">
table { border-collapse: collapse; display: block; width: 100%; overflow: auto; }
td, th { padding: 6px 13px; border: 1px solid #ddd; text-align: right; }
tr { background-color: #fff; border-top: 1px solid #ccc; }
public static class EntityTypeConfigurationExtensions
{
private static readonly MethodInfo EntityMethod = typeof(ModelBuilder).GetTypeInfo().GetMethods().Single(x => x.Name == "Entity" && x.IsGenericMethod && x.GetParameters().Length == 0);
private static readonly IDictionary<Assembly, IEnumerable<Type>> TypesPerAssembly = new Dictionary<Assembly, IEnumerable<Type>>();
private static Type GetEntityType(Type type)
{
Type interfaceType = type.GetInterfaces().First(x => x.GetTypeInfo().IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>));
return interfaceType.GetGenericArguments().First();
}
public class AdventureWorks2014Context : DbContext
{
public AdventureWorks2014Context()
: base("Name=AdventureWorks2014Context")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ShiftMap());
public class AdventureWorksDbContext : DbContext
{
public AdventureWorksDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseEntityTypeConfiguration();
public class ShiftMap : EntityTypeConfiguration<Shift>
{
public ShiftMap()
{
// Table & Column Mappings
this.ToTable("Shift", "HumanResources");
// Primary Key
this.HasKey(t => t.ShiftID);
public sealed class ShiftMap : IEntityTypeConfiguration<Shift>
{
public void Map(EntityTypeBuilder<Shift> builder)
{
// table
builder.ToTable("Shift", "HumanResources");
// keys
builder.HasKey(t => t.ShiftID);
public interface IEntityTypeConfiguration<TEntity> where TEntity : class
{
void Map(EntityTypeBuilder<TEntity> entityTypeBuilder);
}
static class ModelBuilderExtensions
{
private static readonly Assembly EntitiesAssembly = typeof(AdventureWorksDbContext).Assembly;
private static readonly Type EntityActiveableType = typeof(IEntityActiveable);
private static readonly MethodInfo EntityMethod = typeof(ModelBuilder).GetTypeInfo().GetMethods().Single(x => x.Name == "Entity" && x.IsGenericMethod && x.GetParameters().Length == 0);
private static IList<Type> _activeablesTypes;
private static IList<Type> ActiveablesTypes
{
get { return _activeablesTypes ?? (_activeablesTypes = EntitiesAssembly.ExportedTypes.Where(t => t.IsClass && t.IsPublic && EntityActiveableType.IsAssignableFrom(t)).ToList()); }
public class BaseController : Controller
{
protected readonly AdventureWorksDbContext _adventureWorksDbContext;
protected static readonly JsonSerializerSettings DefaultJsonSerializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
public class AdventureWorksDbContext : DbContext
{
public AdventureWorksDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// table