I learned MongoDB recently and I wanted to try it out in C#. I have managed to make the Update and Delete functions work. I am having issues with the Create function.
So, I have three classes named Program, Repository, User.
In Repository class, I have:
public async Task CreateUser(ConsoleApplication1.Models.User user)
{
await _collection.InsertOneAsync(user);
}
In Program, I have following code:
ConsoleApplication1.Models.User new_user; // Declaring Member of User class
string newUserId = Console.ReadLine(); // Taking Input For ID
new_user.id = new ObjectId(newUserId); // ID
new_user.First_Name = Console.ReadLine(); // First Name
new_user.Last_Name = Console.ReadLine(); // Last Name
new_user.Age = Console.ReadLine(); // Age
new_user.Address = Console.ReadLine(); // Address
r1.CreateUser(new_user);
Since in Repository class, the CreateUser method has only one parameter that is the member of the User class and the way I declared the new_user in Program class, it is not initialized, which is why I am getting error on:
new_user.id = Console.ReadLine();
How do I fix this ?