Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created November 30, 2017 08:40
Show Gist options
  • Save lbargaoanu/2ea3d42879f738fc85e76247dacce6e0 to your computer and use it in GitHub Desktop.
Save lbargaoanu/2ea3d42879f738fc85e76247dacce6e0 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
var config = new MapperConfiguration(cfg =>
{
//cfg.CreateMissingTypeMaps = false;
cfg.CreateMap<DTO_Object, EntityClass>().ForMember(s => s.DynamicFields,
o => o.MapFrom(s => s.DynamicFields.Select(m => m.Id)));
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var source = new DTO_Object
{
Id = 1,
MonthlyPricing = 267.23M,
DynamicFields = new []{ new DynamicFieldForm{ Id = 1, ServiceId=111 }, new DynamicFieldForm{ Id = 2, ServiceId=222 }}
};
try
{
var service = mapper.Map<DTO_Object, EntityClass>(source).Dump();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public class DTO_Object
{
public int Id { get; set; }
[Required]
public decimal MonthlyPricing { get; set; }
public IEnumerable<DynamicFieldForm> DynamicFields { get; set; }
}
public class DynamicFieldForm
{
public int Id { get; set; }
public int ServiceId { get; set; }
}
public class EntityClass
{
[Key]
public int Id { get; set; }
public decimal MonthlyPricing { get; set; }
public virtual IEnumerable<DynamicField> DynamicFields { get; set; }
}
public class DynamicField
{
public int Id { get; set; }
[ForeignKey("Service")]
public int ServiceId { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment