title: Auto generate objects Id mongo db c#
body: <p>I learn mongodb with C#, I want to create a record with a list of entities but the child entities didn't generate his object id.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class Curso
  {
      [BsonId]
      public ObjectId _id { get; set; }
  
      [BsonElement("descripcion")]
      public string descripcion { get; set; }
  
      [BsonElement("horas")]
      public int horas { get; set; }
  
      [BsonElement("alumnos")]
      public IEnumerable&lt;Alumno&gt; alumnos { get; set; }
  
      public Curso()
      {
  
      }
  }
  </code></pre>
  
  <p>Alumno entity:</p>
  
  <pre><code>public class Alumno
  {
      [BsonId]
      public ObjectId _id { get; set; }
  
      [BsonElement("name")]
      public string name { get; set; }
  
      [BsonElement("age")]
      public int age { get; set; }
  }
  </code></pre>
  
  <p>I insert a new course, the auto generate id Curso is worked but the Alumno id didn't generate and is set to <code>"000000000000000000000000"</code> instead.</p>
  
  <pre><code>    public Curso create(Curso curso)
      {
          db.GetCollection&lt;Curso&gt;("curso").Save(curso);
          return curso;
      }
  </code></pre>
  
  <p>RESULT: </p>
  
  <pre><code>{ "_id" : ObjectId("5a766ec765583a0f90bc0edf"), "descripcion" : "JAVA + JPA", "horas" : 200, "alumnos" : [ { "_id" : ObjectId("000000000000000000000000"), "name" : "Catalina Castillo", "age" : 22 }, { "_id" : ObjectId("000000000000000000000000"), "name" : "Marcia Pardo", "age" : 45 }, { "_id" : ObjectId("000000000000000000000000"), "name" : "Will Smith", "age" : 24 } ] }
  </code></pre>