[英]How to store incoming data to database using automapper
我正在使用ASP.NET Core 5和Automapper,據我所知,Automapper是為了節省map數據的時間(如果我弄錯了,請糾正我)。 我的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!;
}
我基於上面的 class 構建了我的數據庫表,並且我有 model 類,當我從其他服務獲取數據時使用它,所以我需要 map 這兩個類:
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!;
}
我為這兩個類創建了 map 的配置文件:
public class FinLegCheckProfile:Profile
{
public FinLegCheckProfile()
{
CreateMap<FinLegCheckEntity,FinLegCheckModel>();
}
}
好吧,我希望到目前為止我的方向是正確的,所以我從外部獲取數據,如果數據已經存在於數據庫中,我將更新我的數據庫,否則我將添加,這是我遇到問題的地方:應該如何我使用自動映射器:
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 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();
......
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.