如何保存GUID ID mongodb

时间:2019-04-01 00:11:43

标签: c# asp.net mongodb model-view-controller

我正在尝试在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; }
}

1 个答案:

答案 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/