Created
August 28, 2014 21:12
-
-
Save Rookian/c30128591f3986e2c38c to your computer and use it in GitHub Desktop.
EF AutoMapper Proxy Tests:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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