Last active
April 29, 2019 13:45
-
-
Save tboby/069c56a54b40dbea6d7651f79e90a9a1 to your computer and use it in GitHub Desktop.
Automapper ignore execution plan
This file contains hidden or 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
static void Main(string[] args) | |
{ | |
Mapper.Initialize(cfg => | |
{ | |
cfg.AddProfiles(new Profile[] { new CaseProfile()}); | |
}); | |
var configuration = new MapperConfiguration(cfg => cfg.CreateMap<CaseView, Case>()); | |
var executionPlan = configuration.BuildExecutionPlan(typeof(CaseView), typeof(Case)); | |
// Compare execution plans | |
executionPlan.ToReadableString().Dump(); | |
// I can't reproduce the error I'm seeing with EF proxies yet | |
// var caseView = new CaseView(){ | |
// User = new UserView{ | |
// UserID = 5 | |
// }, | |
// UserID = 5 | |
// }; | |
// var existingCase = new Case(){ | |
// User = new UserChild(){ UserID = 1 } | |
// }; | |
// var result = Mapper.Map<CaseView, Case>(caseView, existingCase); | |
// if (result.User.UserID == 5) { | |
// throw new Exception("Should not have mapped"); | |
// } | |
} | |
public class UserView { | |
public int UserID {get; set;} | |
} | |
public class User { | |
public int UserID { get; set; } | |
} | |
public class UserChild : User { | |
} | |
public class Case | |
{ | |
// 1: Works | |
// [IgnoreMap] | |
// | |
public virtual User User {get; set;} | |
public int? UserID {get; set;} | |
} | |
public class CaseView | |
{ | |
public UserView User {get; set;} | |
public int? UserID {get; set;} | |
} | |
public class CaseProfile : Profile { | |
public CaseProfile() | |
{ | |
CreateMap<CaseView, Case>() | |
// 2 doesn't work | |
// .ForMember(x => x.User, opts => opts.Ignore()) | |
// 3 doesn't work | |
// .ForMember(x => x.User, opts => opts.MapFrom(src => (User)null)) | |
.ForMember(x => x.UserID, opts => opts.MapFrom (src => src.User.UserID)); | |
CreateMap<UserView, User>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Execution plans:
Default (no ignores)
1: Ignoremap attribute:
2/3: Ignore fluent/Null value