Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created September 15, 2017 05:22
Show Gist options
  • Save lbargaoanu/e97aac49d7b333c515cb89886f22ba11 to your computer and use it in GitHub Desktop.
Save lbargaoanu/e97aac49d7b333c515cb89886f22ba11 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Library, LibraryDto>();
});
using(var context = new TestContext())
{
context.Libraries.ProjectTo<LibraryDto>().Dump();
}
}
public class DatabaseInitializer : DropCreateDatabaseAlways<TestContext>
{
protected override void Seed(TestContext context)
{
context.Libraries.AddRange(new[]{new Library { Books = {new Book {}, new Book {}}}});
}
}
public class TestContext : DbContext
{
public TestContext()
{
//Database.Log = s=>Debug.WriteLine(s);
Database.SetInitializer<TestContext>(new DatabaseInitializer());
}
public DbSet<Library> Libraries { get; set; }
}
public class Library
{
public int Id { get; set; }
public List<Book> Books { get; set; }
public Library()
{
Books = new List<Book>();
}
}
public class Book
{
public int Id { get; set; }
}
public class LibraryDto
{
public int Id { get; set; }
public int BooksCount { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment