简体   繁体   English

EF Core + Cosmos Db - 一些构造函数参数 map 但不是其他的

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

I have the following class and data in Cosmos Db.我在 Cosmos Db 中有以下 class 和数据。 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."从数据存储中检索数据时,某些属性在存储的数据中不存在,EF 会将它们 map 作为 null(例如MiddleName ),但DateOfBirthPhysicalAddress参数抛出“可为空的 object 必须具有值”。 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.如果数据存储中未定义/不可用,我希望将任何缺少的属性设置为 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; }
}

Item stored in Cosmos db存储在 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. DateOnly 属性需要在构造函数和记录 class 中标记为可为空。Address 属性不能用于构造函数绑定。 See: Entity types with constructors请参阅: 具有构造函数的实体类型

Final record class:最终记录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