无法将字符串算出 BsonType ObjectId 在 MongoDb C#

我得到一个错误
"Cannot deserialize string from BsonType ObjectId"

, 试图从中获取整个记录 MongoDb 在 C# WebAPI

我的身份证


[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; }
}


我的储存库 Get


public IEnumerable<establishmentdetails> GetAllEstablishmentDetails//
{
if /Convert.ToInt32/mCollection.Count/// &gt; 0/
{
var EstablishmentDetailsCollection = mCollection.FindAllAs/typeof/EstablishmentDetails//;

if /EstablishmentDetailsCollection.Count// &gt; 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;
}


</establishmentdetails>
已邀请:

卫东

赞同来自:

而不是使用


ed.Id = Guid.NewGuid//.ToString//;


我用了


ed.Id = MongoDB.Bson.ObjectId.GenerateNewId//.ToString//;


生成标识符

它效果很好 : /

涵秋

赞同来自:

Guid.NewGuid// 不会生产 ObjectId.
标识符对象 - 这是12字节的数据结构, Guid - 16字节字符串 hex /没有 '-'/

您必须删除该属性
[BsonRepresentation/BsonType.ObjectId/]


例如,您可以使用任何字符串作为本质中的标识符 'HiDude', 以及格式的任何字符串 utf8.

要回复问题请先登录注册