Skip to content

Instantly share code, notes, and snippets.

@Rookian
Created August 28, 2014 21:12
Show Gist options
  • Save Rookian/c30128591f3986e2c38c to your computer and use it in GitHub Desktop.
Save Rookian/c30128591f3986e2c38c to your computer and use it in GitHub Desktop.
EF AutoMapper Proxy Tests:
using System.Data.Entity;
using System.Linq;
using AutoMapper;
namespace EFTests
{
public class Program
{
public static void Main()
{
Mapper.CreateMap<Building, ViewModel>();
using (var context = new MyContext())
{
var building = context.Buildings.FirstOrDefault();
var building1 = new Building();
building.Section = new Section();
// works
var viewModel1 = Mapper.Map<ViewModel>(building1);
// does not work
var viewModel = Mapper.Map<ViewModel>(building);
}
}
}
public class ViewModel
{
public SectionEnum SectionEnum { get; set; }
public ViewModel()
{
}
public ViewModel(SectionEnum section)
{
SectionEnum = section;
}
public string Description { get; set; }
public int Id { get; set; }
}
public class MyContext: DbContext
{
public DbSet<Section> Sections { get; set; }
public DbSet<Building> Buildings { get; set; }
}
public class Section
{
public virtual string Name { get; set; }
public virtual int Id { get; set; }
}
public class Building
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual Section Section { get; set; }
}
public enum SectionEnum
{
A, B, C, D
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment