我正在编写一个 .NET Core Web API,它由 Entity Framework Core 支持,并且底层有一个 PostgreSQL 数据库(AWS Aurora)。我已经能够学习并成功使用 EF Core 进行插入和查询数据,这很棒,但开始研究现有实体的更新,我不清楚实现我所追求的目标的最有效方法。我的数据库实体是我的数据库优先脚手架练习的结果。我有一个类似于下面的实体(简化)。
客户 -< 地址 -< 联系信息
因此,一个客户可能有多个地址和/或多个联系信息记录。我期望我的 Web API 传入一个 JSON 有效负载,该有效负载可以转换为具有所有相关信息的客户。我有一个方法可以将我的 CustomerPayload 转换为可以添加到数据库中的 Customer:
public class CustomerPayload : Payload, ITransform<CustomerPayload, Customer>
{
    [JsonProperty("customer")]
    public RequestCustomer RequestCustomer { get; set; }
    public Customer Convert(CustomerPayload source)
    {
        Console.WriteLine("Creating new customer");
        Customer customer = new Customer
        {
            McaId = source.RequestCustomer.Identification.MembershipNumber,
            BusinessPartnerId = source.RequestCustomer.Identification.BusinessPartnerId,
            LoyaltyDbId = source.RequestCustomer.Identification.LoyaltyDbId,
            Title = source.RequestCustomer.Name.Title,
            FirstName = source.RequestCustomer.Name.FirstName,
            LastName = source.RequestCustomer.Name.Surname,
            Gender = source.RequestCustomer.Gender, …c# postgresql entity-framework entity-framework-core .net-core