24

I am getting error "Cannot deserialize string from BsonType ObjectId" while trying to get all the record from MongoDb in C# WebAPI

My Id is

[BsonId]
public string Id { get; set; }

After Changing it to

[BsonRepresentation(BsonType.ObjectId)] 
public string Id { get; set; }

its working fine

But while i'm calling post method, its giving me different error

"'d05e139c-3a48-4213-bd89-eba0c22c3c6f' is not a valid 24 digit hex string."

How can solve this problem

My Model is:

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

My repository fro Get method

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

My repository for Post method

public EstablishmentDetails Add(EstablishmentDetails ed)
    {
        if (string.IsNullOrEmpty(ed.Id))
        {
            ed.Id = Guid.NewGuid().ToString();
        }

        mCollection.Save(ed);
        return ed;
    }
1
  • 2
    I think your question includes an answer to the question of many people ;) (BsonRepresentation) Commented May 7, 2018 at 1:09

3 Answers 3

19

Instead of using

ed.Id = Guid.NewGuid().ToString();

I used

ed.Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString();

For generating Id

Its working fine : )

Sign up to request clarification or add additional context in comments.

1 Comment

This is worked like a charm.!
2

Guid.NewGuid() will not produce ObjectId. Object Id is 12 byte data structure and Guid produce 16byte hex string (without '-')

You should remove attribute [BsonRepresentation(BsonType.ObjectId)]

You can use any string as Id in your entity for example 'HiDude' and any string in utf8 format.

Comments

0

In my case, string was the appropriate C# datatype, but I accidentally inserted a document (using Studio3T > Paste Document > Import Document).

I intended it to update an existing document based on _id (a plain-English string value like ''ABC_123'`; not a GUID).

But that's not how the Paste Document works... If the imported document has a conflicting _id with an existing document, it will create a new ObjectId('...') value, -- and that is not compatible with C# string datatype...

In my case, since I meant to update , not insert, I deleted my extra document and did an update properly (either a) deleting and pasting/importing, or, b) right-> click "Update document")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.