Skip to content

Commit 5883a64

Browse files
If an int is nullable the source value shouldn't be touched by automapper
1 parent 6b380c6 commit 5883a64

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/Presentation/SmartStore.Web/Administration/Infrastructure/AutoMapperAdminProfile.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,33 @@ namespace SmartStore.Admin.Infrastructure
4646
{
4747
public class AutoMapperAdminProfile : Profile
4848
{
49-
class OptionalFkConverter : ITypeConverter<int, int?>
49+
class OptionalFkConverter : ITypeConverter<int?, int?>, ITypeConverter<int, int?>
5050
{
51-
public int? Convert(int source, int? destination, ResolutionContext context)
52-
{
53-
//var srcName = context.PropertyMap.SourceMember.Name;
54-
55-
//if (context.PropertyMap.SourceMember.MemberType == MemberTypes.Property && srcName.EndsWith("Id") && !context.SourceType.IsNullable())
56-
//{
57-
// var src = (int)context.SourceValue;
58-
// return src == 0 ? (int?)null : src;
59-
//}
51+
public int? Convert(int? source, int? destination, ResolutionContext context)
52+
{
53+
return source;
54+
}
6055

61-
//return (int?)context.SourceValue;
62-
63-
return source == 0 ? (int?)null : source;
56+
public int? Convert(int source, int? destination, ResolutionContext context)
57+
{
58+
return source == 0 ? (int?)null : source;
6459
}
6560
}
6661

67-
public AutoMapperAdminProfile()
62+
public AutoMapperAdminProfile()
6863
{
69-
// TODO remove 'CreatedOnUtc' ignore mappings because now presentation layer models have 'CreatedOn' property and core entities have 'CreatedOnUtc' property (distinct names)
64+
// TODO remove 'CreatedOnUtc' ignore mappings because now presentation layer models have 'CreatedOn' property and core entities have 'CreatedOnUtc' property (distinct names)
7065

71-
// special mapper, that avoids DbUpdate exceptions in cases where
72-
// optional (nullable) int FK properties are 0 instead of null
73-
// after mapping model > entity.
74-
CreateMap<int, int?>().ConvertUsing(new OptionalFkConverter());
66+
// special mapper, that avoids DbUpdate exceptions in cases where
67+
// optional (nullable) int FK properties are 0 instead of null
68+
// after mapping model > entity.
69+
// if type is nullable source value shouldn't be touched
70+
var fkConverter = new OptionalFkConverter();
71+
CreateMap<int?, int?>().ConvertUsing(fkConverter);
72+
CreateMap<int, int?>().ConvertUsing(fkConverter);
7573

76-
//address
77-
CreateMap<Address, AddressModel>()
74+
//address
75+
CreateMap<Address, AddressModel>()
7876
.ForMember(dest => dest.AddressHtml, mo => mo.Ignore())
7977
.ForMember(dest => dest.AvailableCountries, mo => mo.Ignore())
8078
.ForMember(dest => dest.AvailableStates, mo => mo.Ignore())

0 commit comments

Comments
 (0)