简体   繁体   中英

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

I have the following class and data in Cosmos Db. When retrieving the data from the data store, some of the properties don't exist in the stored data and EF will map them as null (such as MiddleName ) but the DateOfBirth and PhysicalAddress parameters throw a "Nullable object must have a value." error. I can't find out why the User parameters are being handled differently. I want any missing properties to be set to null if undefined/unavailable in the data store.

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; }
}

Item stored in 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"
}

Call to retrieve the all users

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

For anyone that may run into this issue. The DateOnly property needed to be marked as nullable in the constructor and in the record class. The Address property cannot be used in constructor binding. See: Entity types with constructors

Final record 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM