如何使用automapper将传入数据存储到数据库

How to store incoming data to database using automapper

我用的是ASP.NETCore 5和Automapper,据我了解,Automapper是利用省时省力的方式将数据一一映射(如有误请指正)。我的 DbContext:

中有这些实体 classes
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!;
}

我的数据库table是基于上面的class构建的,我有模型classes,我在从其他服务获取数据时使用它,所以我需要映射这两个 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!;
}

我创建了一个配置文件来映射这两个 classes:

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

好吧,我希望到目前为止我的方向是正确的!所以我从外部获取数据,如果数据已经存在于数据库中,我将更新我的数据库,否则我将添加,这是我遇到问题的地方,我应该如何使用 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);
}

我已将 IMapper 注入 class 并且我可以访问 _mapper.Map 但问题是如何在映射后存储或更新数据库?

我认为您不需要任何 DTO,因为它与实体相同 class。但无论如何请尝试此代码。您可以在使用前重新映射您的数据。

public bool AddOrUpdateFinLegCheckEntity (FinLegCheckEntity model)
{
if ( model.CustomerId > 0 )
{
var existedFincheckData = dbContext.FinLegCheckEntities.FirstOrDefault(s => s.CustomerId == model.CustomerId);

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

......
return true;
}