On this article, we’ll find out how we will do the MongoDB primary integration with Visible Studio 2015. Earlier than we get began, if you wish to learn about export GridView information to excel, please undergo the next article: Export GridView to excel with advanced features.
First, we create a brand new undertaking named “MongoDBTest” and we’ll select a console software. Now, we have to add the references for MongoDB. We will use the NuGet package deal supervisor choice.
Then, browse for “MongoDB” and set up the next two references. Now the appliance is nicely set for utilizing MongoDB. Let’s begin coding…
First, we’ll create a website class named “Worker” as follows:
1 2 3 4 5 6 7 8 9 10 11 |
public class Worker { [BsonId] public string EmployeeNo { get; set; } public string FirstName { get; set; } public string LastName { get; set; } [BsonIgnoreIfNull] public int? Age { get; set; } } |
Right here, we use “[BsonId]” which is the default ID property for MongoDB, and the “[BsonIgnoreIfNull]” token is to keep away from the null value-adding.
Second, we’ll create a Context class inside this system.cs class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class EmployeeContext { non-public MongoDatabase db; public EmployeeContext() { MongoClient consumer = new MongoClient(); var server = consumer.GetServer(); this.db = server.GetDatabase("HR"); var assortment = db.GetCollection<Worker>("Worker"); } public MongoCollection<Worker> Workers => db.GetCollection<Worker>("Worker"); } |
Right here, we now have entry to the “HR” database and we’ll do some actions within the “Worker” collections. Now, We are going to replace or add data to our MongoDB.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Program { static void Fundamental(string[] args) { EmployeeContext ctx = new EmployeeContext(); Worker aEmployee = new Worker { EmployeeNo = "B0234235", FirstName = "Ahsan", LastName = "Rupom", Age = 28 }; ctx.Workers.Save(aEmployee); aEmployee = ctx.Workers.AsQueryable().First(); Console.WriteLine("EmployeeNo : " + aEmployee.EmployeeNo); Console.WriteLine("FirstName : " + aEmployee.FirstName); Console.WriteLine("LastName : " + aEmployee.LastName); Console.WriteLine("Age : " + aEmployee.Age); } } |
So, as we will see it is extremely straightforward to combine MongoDB into the visual studio. Ensure that your MongoDB server is operating. I hope you loved this quite simple MongoDB integration with visible studio and c#. Click on right here to obtain the supply. Thanks.
Nice post for beginner… Thanks Rupom Vai
Thanks a lot for your comment