簡體   English   中英

EF Core + Cosmos Db - 一些構造函數參數 map 但不是其他的

[英]EF Core + Cosmos Db - some constructor parameters map but not others do not

我在 Cosmos Db 中有以下 class 和數據。 從數據存儲中檢索數據時,某些屬性在存儲的數據中不存在,EF 會將它們 map 作為 null(例如MiddleName ),但DateOfBirthPhysicalAddress參數拋出“可為空的 object 必須具有值”。 錯誤。 我無法找出為什么用戶參數的處理方式不同。 如果數據存儲中未定義/不可用,我希望將任何缺少的屬性設置為 null。

public class User
    {
private User(UserId userId, IdpId idpId, string firstName, string middleName, string lastName, string suffix,
            string email, string phoneNumber, DateOnly dateOfBirth)
        {
            UserId = userId;
            IdpId = idpId;
            FirstName = firstName;
            MiddleName = middleName;
            LastName = lastName;
            Suffix = suffix;
            Email = email;
            PhoneNumber = phoneNumber;
            DateOfBirth = dateOfBirth;
        }
        public UserId UserId { get; } = UserId.New();
        public IdpId IdpId { get; set; }
        public string FirstName { get; set; }

        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string Suffix { get; set; }
        public string Email { get; set; }
        public Address PhysicalAddress { get; set; }
        public string PhoneNumber { get; set; }

        public DateOnly DateOfBirth { get; set; }
        public string ETag { get; set; }
    }
public class Address
{
    public string StreetLine1 { get; init; }
    public string StreetLine2 { get; init; }
    public string City { get; init; }
    public string State { get; init; }
    public static string Country => "US";
    public string PostalCode { get; init; }
}

存儲在 Cosmos db 中的項目

{
  "UserId": "C8282366-A13C-48DF-9893-A5400DD73264",
  "Created": "2021-06-27T12:09:54.556736-04:00",
  "CreatedBy": "C8282366-A13C-48DF-9893-A5400DD73264@clients",
  "Discriminator": "User",
  "Email": "bob@bob.com",
  "FirstName": "Bob",
  "IdpId": "google-oauth2|111111111111111111111",
  "LastModified": "2021-06-28T22:16:39.068558-04:00",
  "LastModifiedBy": "C8282366-A13C-48DF-9893-A5400DD73264@clients",
  "LastName": "King",
  "id": "User|C8282366-A13C-48DF-9893-A5400DD73264"
}

調用以檢索所有用戶

var userList = await _dbContext.Users.ToListAsync(cancellationToken);

對於可能遇到此問題的任何人。 DateOnly 屬性需要在構造函數和記錄 class 中標記為可為空。Address 屬性不能用於構造函數綁定。 請參閱: 具有構造函數的實體類型

最終記錄class:

public record User
    {
private User(UserId userId, IdpId idpId, string firstName, string middleName, string lastName, string suffix,
            string email, string phoneNumber, DateOnly? dateOfBirth)
        {
            UserId = userId;
            IdpId = idpId;
            FirstName = firstName;
            MiddleName = middleName;
            LastName = lastName;
            Suffix = suffix;
            Email = email;
            PhoneNumber = phoneNumber;
            DateOfBirth = dateOfBirth;
        }
        public UserId UserId { get; } = UserId.New();
        public IdpId IdpId { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string Suffix { get; set; }
        public string Email { get; set; }
        public Address PhysicalAddress { get; set; }
        public string PhoneNumber { get; set; }
        public DateOnly? DateOfBirth { get; set; } 
        public string ETag { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM