C# MongoDB strong type class independent of MongoDB

Hi i want use clear C# class without reference\independent to mongodb. Id is Guid. Now my main problem is dublicate ids, example:

public class Car
    {
        public Guid CarId { get; set; }
        public string Name { get; set; }
    }

And on init conventio:
var pack = new ConventionPack {new IgnoreExtraElementsConvention(true)};

Default use case:
cars.find(c => c.CarId == carId).FirstOfDefault();

In database i have never used _id field

image


Can use only one field id, clear class (without ref\using\attribute\etc) with C# Guid?

Examble what need:

public class Car
    {
        public Guid _id { get; set; }
        public string Name { get; set; }
    }

ConventionRegistry.Register( default_id_field_name = ‘_id’ );
ConventionRegistry.Register( default_id_type = Guid);

image

Hi @alexov_inbox,

I’m not quite sure what you’re looking for. If you’re looking to have an id field _id with the value of Guid instead of the default ObjectId, you try the following example:

 public class Car 
 {
     public Guid Id {get; set;}
     public string Name {get; set;}
 }

On insert, this should create a document example as below:

{ 
    "_id" : CSUUID("fe664c82-a26c-48b0-88ca-b94e6e11805d"), 
    "Name" : "foo" 
}

See also BSON Mapping: The Id Member for more information.

Regards,
Wan.

1 Like

oh… I try this a few years ago and nothing work with many exceptions…
Today with last mongodb.driver all work from ‘box’ how need in my question.
Thx! Sorry to hurry! :smile:

Hi @alexov_inbox,

Not a problem, I’m glad that your question is answered.

Best regards,
Wan.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.