我在try 从C#WebAPI中的MongoDb获取所有记录时遇到错误"Cannot deserialize string from BsonType ObjectId"
我的身份证是
[BsonId]
public string Id { get; set; }
改成
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
很好用
但当我调用post方法时,它给了我不同的错误
"'d05e139c-3a48-4213-bd89-eba0c22c3c6f' is not a valid 24 digit hex string."
如何解决这个问题
我的模型是:
public class EstablishmentDetails
{
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string EstablishmentName { get; set; }
public string EstablishmentType { get; set; }
public string Address { get; set; }
public string City { get; set; }
public int StateID { get; set; }
public Int32 PIN { get; set; }
public Int64 PhoneNumber { get; set; }
public string EmailID { get; set; }
public bool Published { get; set; }
public string CreatedDate { get; set; }
public string ModifiedDate { get; set; }
}
我的存储库fro Get方法
public IEnumerable<EstablishmentDetails> GetAllEstablishmentDetails()
{
if (Convert.ToInt32(mCollection.Count()) > 0)
{
var EstablishmentDetailsCollection = mCollection.FindAllAs(typeof(EstablishmentDetails));
if (EstablishmentDetailsCollection.Count() > 0)
{
foreach (EstablishmentDetails item in EstablishmentDetailsCollection)
{
establishmentDetails.Add(item);
}
}
}
var results = establishmentDetails.AsQueryable();
return results;
}
我的Post方法存储库
public EstablishmentDetails Add(EstablishmentDetails ed)
{
if (string.IsNullOrEmpty(ed.Id))
{
ed.Id = Guid.NewGuid().ToString();
}
mCollection.Save(ed);
return ed;
}