![](/img/trans.png)
[英]How do i setup RBAC gremlin cosmos db DefaultAzureCredentials
[英]EF Core + Cosmos Db - some constructor parameters map but not others do not
我在 Cosmos Db 中有以下 class 和数据。 从数据存储中检索数据时,某些属性在存储的数据中不存在,EF 会将它们 map 作为 null(例如MiddleName
),但DateOfBirth
和PhysicalAddress
参数抛出“可为空的 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.