Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code snippets for the blog post /2017/01/24/entity-framework-core-shadow-properties/
using DataModels;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
namespace Services
{
public class BookService : IBookService
{
private ExampleContext _context;
private readonly string NameOfCreatedField = "Created";
public BookService (ExampleContext context)
{
_context = context;
}
public Book FindById(int id)
{
return BaseQuery().FirstOrDefault(book => book.BookId == id);
}
public IEnumerable<Book> GetAll()
{
return BaseQuery();
}
private IEnumerable<Book> BaseQuery()
{
return _context.Books.OrderBy(b => EF.Property<DateTime>(b, NameOfCreatedField));
}
}
}
private IEnumerable<Book> BaseQuery()
{
return _context.Books.AsNoTracking().OrderBy(b => EF.Property<DateTime>(b, NameOfCreatedField));
}
namespace DataModels
{
public class Book : IAuditable
{
public int BookId { get; set; }
public string BookName { get; set; }
}
}
using DataModels;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
namespace Services
{
public class BookService : IBookService
{
private ExampleContext _context;
public BookService (ExampleContext context)
{
_context = context;
}
public Book FindById(int id)
{
return _context.FirstOrDefault(book => book.BookId == id);
}
public IEnumerable<Book> GetAll()
{
return _context.Books.OrderBy(b => EF.Property<DateTime>(b, "Created"));
}
}
}
using DataModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace DataLayer.Contexts
{
public class ExampleContext : DbContext
{
public ExampleContext(DbContextOptions<DwContext> options) : base(options) { }
public ExampleContext() { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Create shadow properties
foreach (var entityType in modelBuilder.Model.GetEntityTypes()
.Where(e => typeof(IAuditable).IsAssignableFrom(e.ClrType)))
{
modelBuilder.Entity(entityType.ClrType)
.Property<DateTime>("Created");
modelBuilder.Entity(entityType.ClrType)
.Property<DateTime>("Modified");
modelBuilder.Entity(entityType.ClrType)
.Property<string>("CreatedBy");
modelBuilder.Entity(entityType.ClrType)
.Property<string>("ModifiedBy");
}
base.OnModelCreating(modelBuilder);
}
public override int SaveChanges()
{
ApplyAuditInformation();
return base.SaveChanges();
}
private void ApplyAuditInformation()
{
var modifiedEntities = ChangeTracker.Entries<IAuditable>()
.Where(e => e.State == EntityState.Added || e.State == EntityState.Modified);
foreach (var entity in modifiedEntities)
{
entity.Property("Modified").CurrentValue = DateTime.UtcNow;
if (entity.State == EntityState.Added)
{
entity.Property("Created").CurrentValue = DateTime.UtcNow;
}
}
}
public DbSet<Book> Books { get; set; }
}
}
namespace DataModels
{
public interface IAuditable
{
}
}
using DataModels;
using System.Collections.Generic;
namespace Services
{
public interface IBookService
{
Book FindById(int id);
IEnumerable<Book> GetAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment