我正在尝试在mongodb中保存一个"guid id"
,但是当我保存它时,它会另存为二进制文件。
当您使用带有guid的get时,找不到它。
如何以相同的guid
格式保存?
我希望您如何保存:ownerId: "b0164f61-a203-478b-becc-b9a67312981d"
他的储蓄方式:ownerId: Binary('sBZPYaIDR4u+zLmmcxKYHQ==')
我的ViewModel
public class EmployeeViewModel
{
[BsonId]
public Guid ownerId { get; set; }
[BsonElement("personalData")]
public PersonalDataViewModel personalData { get; set; }
[BsonElement("address")]
public AddressViewModel address { get; set; }
}
答案 0 :(得分:2)
为BsonId指定IdGenerator。
public class EmployeeViewModel
{
[BsonId(IdGenerator=typeof(StringObjectIdGenerator))]
public Guid ownerId { get; set; }
[BsonElement("personalData")]
public PersonalDataViewModel personalData { get; set; }
[BsonElement("address")]
public AddressViewModel address { get; set; }
}
https://codingcanvas.com/using-mongodb-_id-field-with-c-pocos/