简体   繁体   中英

How to store incoming data to database using automapper

I'm using ASP.NET Core 5 and Automapper, as far as I learned, Automapper is to use saving time to map data one by one (please correct me if I'm mistaken). I have these entity classes in my DbContext :

public class FinLegCheckEntity
{
    [Key]
    public int Id { get; set; }
    public int CustomerId { get; set; } = default!;
    public string? CustomerRef { get; set; } = default!;
    public int KYClevel { get; set; } = default!;
    public List<PlatformKYCLevels> PlatformKycLevels { get; set; } = default!;
    public DateTimeOffset Created { get; set; }
    public DateTimeOffset Modified { get; set; }
}

public class PlatformKYCLevels
{
    [Key]
    public int Id { get; set; }
    public int Level { get; set; } = default!;
    public string MaxAmount { get; set; } = default!;
    public string Currency { get; set; } = default!;
}

I built my database table based on the class above, and I have model classes, I use it when I get data from other service, so I need to map these two classes:

public class FinLegCheckModel
{
    public int Id { get; set; }
    public int CustomerId { get; set; } = default!;
    public string? CustomerRef { get; set; } = default!;
    public int KYClevel { get; set; } = default!;
    public List<PlatformKYCLevels> PlatformKycLevels { get; set; } = default!;
    public DateTimeOffset Created { get; set; }
    public DateTimeOffset Modified { get; set; }
}

public class PlatformKYCLevels
{
    public int Id { get; set; }
    public int Level { get; set; } = default!;
    public string MaxAmount { get; set; } = default!;
    public string Currency { get; set; } = default!;
}

I created a profile to map these two classes:

public class FinLegCheckProfile:Profile
{
    public FinLegCheckProfile()
    {
        CreateMap<FinLegCheckEntity,FinLegCheckModel>();
    }
}

Well, I hope I'm in the right direction so far, So I get data from outside, if the data is already exist in the database I will update my db otherwise I will add, here is where I have a problem: how should I use Automapper:

var customerFincheckData = dbContext.FinLegCheckEntities.FirstOrDefault(s => s.CustomerId == fincheckdata.CustomerId);

if (customerFincheckData != null)
{
    customerFincheckData.CustomerId = fincheckdata.CustomerId;
    customerFincheckData.KYClevel = fincheckdata.KYClevel;
    customerFincheckData.PlatformKycLevels = fincheckdata.PlatformKycLevels;
    customerFincheckData.CustomerRef = fincheckdata.CustomerRef;
    customerFincheckData.Modified = DateTimeOffset.UtcNow;
                         
    dbContext.FinLegCheckEntities.Update(customerFincheckData);
    dbContext.SaveChanges();

    return Task.FromResult(true);
}
else
{
    var finlegEntities = new FinLegCheckEntity()
                             {
                                 CustomerId = fincheckdata.CustomerId,
                                 KYClevel = fincheckdata.KYClevel,
                                 PlatformKycLevels = fincheckdata.PlatformKycLevels,
                                 CustomerRef = fincheckdata.CustomerRef,
                                 Created = DateTimeOffset.UtcNow,
                                 Modified = DateTimeOffset.UtcNow
                              };
    dbContext.FinLegCheckEntities.Add(finlegEntities);
    dbContext.SaveChanges();

    return Task.FromResult(true);
}

I have injected IMapper into the class and i have access to _mapper.Map but the problem is how can store or update the db after mapping?

I don't think that you need any DTO since it is the same as an entity class. Try this

public ActionResult AddOrUpdate(FinLegCheckEntity model)
{
var existedFincheckData = dbContext.FinLegCheckEntities.FirstOrDefault(s => s.CustomerId == fincheckdata.CustomerId);

if (existedFincheckData != null)
{
    model.Modified = DateTimeOffset.UtcNow;
    dbContext.Entry(existedFincheckData).CurrentValues.SetValues(model);
}
else
{
    model.Created = DateTimeOffset.UtcNow,
     model.Modified = DateTimeOffset.UtcNow
     dbContext.FinLegCheckEntities.Add(model);
}
 dbContext.SaveChanges();

......
}

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