简体   繁体   中英

Error System.Nullable'1 automapper

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.

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM