1

i have entity class, pinjaman_DataEntities, that has 4 properties.

public string appl_no { get; set; }

public string reff_number { get; set; }

public string cust_name { get; set; }

public string merchant_id { get; set; }

and i have a database context gt_applikasi_pinjaman that has more properties than pinjaman_DataEntities, but still have same name of 4 properties above.

and i want to map from pinjaman_DataEntities to gt_appllikasi_pinjaman

and this codes below :

public bool updateFilter(pinjaman_DataEntities filterPinjaman)
{    
    bool valid = true;    
    filterPinjaman = (pinjaman_DataEntities)ConvertDataEmpetyStringToNull(filterPinjaman);    
    gt_applikasi_pinjaman pinjaman = dbContext.gt_applikasi_pinjaman.Find(filterPinjaman.appl_no); 

    Mapper.CreateMap<pinjaman_DataEntities, gt_applikasi_pinjaman>()
         .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

    Mapper.Map(filterPinjaman, pinjaman);    
    dbContext.Entry(pinjaman).State = EntityState.Modified;    
    dbContext.SaveChanges();    
    return valid;    
}

after i run the code, i have error when the code was on

Mapper.Map(filterPinjaman, pinjaman);

And the error message is :

An exception of type 'System.Exception' occurred in WebApplicationServiceAPIA.dll but was not handled in user code

Additional information:

Mapping types:

pinjaman_DataEntities -> Nullable`1

ServiceAPIA.DataEntities.pinjaman_DataEntities -> System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

Destination path: gt_applikasi_pinjaman.appl_date.appl_date

Source value: ServiceAPIA.DataEntities.pinjaman_DataEntities

What's that mean, and how to fix it? Lot of thanks.

Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
M. Fahrizal
  • 15
  • 10
  • can you pls let us know property `appl_date.appl_date` what type it is and its available in both or only in `gt_applikasi_pinjaman` – Deepak Sharma Feb 28 '18 at 03:40
  • public Nullable appl_date { get; set; } it's only on gt_applikasi_pinjaman – M. Fahrizal Feb 28 '18 at 03:47
  • you are using `Condition(srs => !srs.IsSourceValueNull)` but this property `IsSourceValueNull` is not mentioned in you source model. you source model has only 4 property you mentioned?? I doubt.. – Deepak Sharma Feb 28 '18 at 04:25
  • and when i add this property public Nullable appl_date { get; set; }, to _pinjaman_DataEntities_, the error message is change to pinjaman_DataEntities -> Decimal Destination path: gt_applikasi_pinjaman.principal_loan.principal_loan Source value: ServiceAPIA.DataEntities.pinjaman_DataEntities the property : public decimal principal_loan { get; set; } – M. Fahrizal Feb 28 '18 at 04:27
  • what exactly you want to do? you want to copy the these 4 property to you destination that's it right? you want to exclude the property if new value is `null or empty` or you still want to assign? – Deepak Sharma Feb 28 '18 at 04:46
  • Yes sir, in pinjaman_DataEntities i put 4 properties, and in gt_applikasi_pinjaman, there are 40 properties. that's why i use Condition(srs => !srs.IsSourceValueNull), on purpose it mapped data only when source is not null. Because technically it's only 4 data from pinjaman_DataEntities that will be mapped to gt_applikasi_pinjaman. Except that will be passed. But, i wonder why the error is on data type except string. i have property that data type is string, even it's null. But there's no exception. But, it goes to error when data type such decimal, date.time. – M. Fahrizal Feb 28 '18 at 04:49
  • its working fine for me, and btw I am using latest version of `automapper` `6.2.2.0`. My source has 4 and destination has 5 prop. and its working fine for me. – Deepak Sharma Feb 28 '18 at 04:56
  • what exactly you want to do? you want to copy the these 4 property to you destination that's it right? you want to exclude the property if new value is null or empty or you still want to assign? Yes, Sir. i want to assign these 4 properties. But there's error on properties in gt_applikasi_pinjaman(destiny) that has data type except string. Because, i have data type string and it's null, but it's not error and skipped. – M. Fahrizal Feb 28 '18 at 05:01
  • can you send me the code like this Condition(srs => !srs.IsSourceValueNull) in latest version or it still same code? is your destiny properties has data type decimal or date.time? – M. Fahrizal Feb 28 '18 at 05:03
  • check the below link. https://dotnetfiddle.net/VBUTCu – Deepak Sharma Feb 28 '18 at 05:08
  • DONE Thanks a lot to Mr Deepak Sharma. The case is : i install automapper version 3.1.1, and this code, "Condition(srs => !srs.IsSourceValueNull)" is doesn't work well, to make condition for nullable value, so the properties that has null value except data type string still proceed by it. (i dont know why) But, after i install automapper latest version 6.2.2.0, and i use the code from Mr Deepak Sharma "opt.Condition(src => src != null))", it work. It skipped all null value for any data type, date, decimal, or string on destiny entity. But, still assign new value from source. – M. Fahrizal Feb 28 '18 at 07:51

1 Answers1

0

I am using auto mapper 6.2.2.0 latest release. and in this version to skip the null you need to use -

.ForAllMembers(opt => opt.Condition(src => src != null))

check the below code if it works for you.. Fiddle

using System;
using AutoMapper;

public class Program
{
    public static void Main(string[] args)
    {
        var sVm = new SourceVM
        {
            cust_name = "Gaurav",
            appl_no = "HR99TEMP5253"
        };

        var r = MyConvert(sVm);
        Console.WriteLine(r.cust_name);
        Console.WriteLine(r.appl_no);
        Console.WriteLine(r.appl_date);


        r = MyConvert(null);
        Console.WriteLine(r.cust_name);
        Console.WriteLine(r.appl_no);
        Console.WriteLine(r.appl_date);

    }


    private static DestinationVM GetDest()
    {
        return new DestinationVM
        {
            cust_name = "Deepak",
            appl_no = "HR26DK6149",
            appl_date = DateTime.Now
        };
    }
    public static DestinationVM MyConvert(SourceVM vm)
    {            

        DestinationVM destVm = GetDest();

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<SourceVM, DestinationVM>()
            .ForAllMembers(opt => opt.Condition(src => src != null));
        });

        config.CreateMapper().Map(vm, destVm);

        return destVm;
    }
}


public class SourceVM
{
    public string appl_no { get; set; }

    public string reff_number { get; set; }

    public string cust_name { get; set; }

    public string merchant_id { get; set; }
}

public class DestinationVM
{
    public string appl_no { get; set; }

    public string reff_number { get; set; }

    public string cust_name { get; set; }

    public string merchant_id { get; set; }

    public Nullable<System.DateTime> appl_date { get; set; }
}
Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31