PostgreSQL
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> PostgreSQL

Entity Framework Core - cách hiệu quả để cập nhật Entity có con dựa trên biểu diễn JSON của thực thể được chuyển vào qua Web API

Bạn đang làm việc với đồ thị của các thực thể bị ngắt kết nối. Đây là phần tài liệu mà bạn có thể quan tâm.

Ví dụ:


var existingCustomer = await loyalty.Customer
    .Include(c => c.ContactInformation)
    .Include(c => c.Address)
    .Include(c => c.MarketingPreferences)
    .Include(c => c.ContentTypePreferences)
    .Include(c => c.ExternalCards)
    .FirstOrDefault(c => c.McaId == customer.McaId);

if (existingCustomer == null)
{
    // Customer does not exist, insert new one
    loyalty.Add(customer);
}
else
{
    // Customer exists, replace its property values
    loyalty.Entry(existingCustomer).CurrentValues.SetValues(customer);

    // Insert or update customer addresses
    foreach (var address in customer.Address)
    {
        var existingAddress = existingCustomer.Address.FirstOrDefault(a => a.AddressId == address.AddressId);

        if (existingAddress == null)
        {
            // Address does not exist, insert new one
            existingCustomer.Address.Add(address);
        }
        else
        {
            // Address exists, replace its property values
            loyalty.Entry(existingAddress).CurrentValues.SetValues(address);
        }
    }

    // Remove addresses not present in the updated customer
    foreach (var address in existingCustomer.Address)
    {
        if (!customer.Address.Any(a => a.AddressId == address.AddressId))
        {
            loyalty.Remove(address);
        }
    }
}

loyalty.SaveChanges();




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Xóa dòng ngắt khỏi đầu ra php json

  2. Múi giờ truy vấn - máy chủ db và máy khách có múi giờ khác nhau

  3. Có thể sử dụng một đối tượng Array làm tham số trong Spring Repository @Query annotation không?

  4. Bộ lọc truy vấn Django sử dụng mảng id lớn trong Postgres DB

  5. Postgres nhúng cho các bài kiểm tra khởi động mùa xuân