preface

, the first two days in learning MongoDB related knowledge, made a small Demo, doing the following provinces, how many schools, well, do rough.

we see in the official document of MongoDB that the driver of more than 2.4 of MongoDb's For.Net is to support.Net Core 2.

src=

, so after we have installed the http://files.jb51.net/file_images/article/201802/201823112421231.png, we can start the journey of the" zero ". The following

MongoDB connection method: first to use Nuget to add a MongoDB package, download the package

after installation, start writing code, create a province entity, a

entity

 using MongoDB.Bson.Serialization.Attributes school; using System.Collections.Generic; namespace MongoCore.Models {public class Province {[BsonId] public int} ProvinceID {get; set; public string ProvinceName {get set; } / / / / / / < summary> provinces there are a number of schools here set to keep < /summary> public; / / /; IList< School> SchoolName {get}}}; set; namespace MongoCore.Models {/ / add School (string schoolName public school, string years) {SchoolName = schoolName; Years = years;} public class School {public string} SchoolName {get; set; public string Years {get}}; set;}

MongoDB

 to create the context class, with namespace MongoCore.Models public class ProvinceContext {{/ / private readonly IMongoDatabase _database database (public = null; ProvinceContext) {/ / connection server name Mongo default port 27017 var client new = MongoClient ("mongodb://.......: 27017" if (clien); T! = null) / / _database = client.GetDatabase ("connect to the database database name");} public IMongoCollection< Province> Province {get {return _database.GetCollection< Province> ("Province");}}}}

create

 private controller 

readonly ProvinceContext _context = new ProvinceContext (public); async Task< IActionResult> Index (VaR) {list = await (_context.Province.Find _ => true (.ToListAsync)); return View (list); /pre>

}< view @model List< MongoCore.Models.Province> ViewData[; @{] "Title" = "Index"}; < h2> Index< /h2> < h2> Index< /h2> < a asp-action= "Create" > < input; type= "button" value= "new" /> < /a> < table> < tr> < th> /th> < province of ID< th> /th> province name < < th> < /th> < /tr> @foreach (VaR item in Model) {< tr> < td> @Html.DisplayFor (modelItem => item.ProvinceID & lt; /td>); < td> @Html.DisplayFor (modelItem => item.ProvinceName < /td>); < td> < a asp-action= "Insert" asp-route-ProvinceID= "@item.ProvinceID" > new < /a> < a asp-action= "Detail" asp-route-ProvinceID= "@item.ProvinceID" > < /a> details; < a asp-action= "Delete" asp-route-ProvinceID= "@item.ProvinceID > < /a> deletion; < /td> < /tr> < /table>}; when 

is running in the Startup.cs

 20181311200? >

operation effect is such that there is no data,

? Click the new button to add here I added the provinces, Hubei province

added 20181311214 provinces code as follows:

 public IActionResult Create (back 

) {return (View) [HttpPost] [ValidateAntiForgeryToken] public async}; Task< ActionResult> Create (Province item) {try {/ / initial chemical type data item.School Name = new List< School> await; (_context.Province.InsertOneAsync) (item); return RedirectToAction (NAMEOF (Index));} catch {return}); View (}

 @model MongoCore.Models.Province @{view: ViewData["Title"] = "Create"}; < h2> Create< /h2> < div> < div> < form asp-action= "Create" > < div; asp-validation-summary= "ModelOnly" > < /div> < div> < label> /label> < province of ID& lt; input; asp-for= "ProvinceID" /> < /div> < div> < label> Province the name < /label> < input asp-for= "ProvinceName" /> < /div> < div> < input type= "submit" value= "save" /> < /div> < /form> < /div> < /div> 

; next is to add the following school

 Province Public async Task< IActionResult> Insert (int ProvinceID) {var num = await _context.Province.Find (P => p.ProvinceID = = ProvinceID) (.SingleOrDefaultAsync); return View (Num) [HttpPost] [ValidateAntiForgeryToken] public async}; Task< IActionResult> Insert (int ProvinceID, string Years string, SchoolName) {var item = await _context.Province.Find (P => p.ProvinceID = = ProvinceID) (.SingleOrDefaultAsync); School SL = new School (SchoolName, Years); / / add school item.SchoolName.Add (SL); / / actionResult = await _context.Province.ReplaceOneAsync update ReplaceOneResult (n => n.ProvinceID.Equals (ProvinceID), item, new Upd AteOptions {IsUpsert} = true); return RedirectToAction (NAMEOF (Index));}

 @model MongoCore.Models.Province @{view: ViewData["Title"] = "Insert"}; < h2> /h2> < new < div> < div> < form asp-action= > < "Insert" div "ModelOnly" > asp-validation-summary=; < /div> < input type= "hidden" asp-for= "ProvinceID" /> < div> < label> /label> school name < < input name= "SchoolName" /> < /div> < div> < label> < was set up; /label> < input name= "Years" /> < /div> < div> < input type= "submit" value= /> < "save"; < /div&; gt; /form> < /div> < /div> 

and then add the school, I Tim With two schools, you can see in the MongoDB,

style= #ff0000" >

all above is the summary of this article, I hope this paper has some reference value of learning to learn or, if there is any doubt you can exchange messages, thank you for the support of the script.


This concludes the body part

This paper fixed link:http://www.script-home.com/the-detailed-steps-of-the-net-core-connection-to-the-mongodb-database.html | Script Home | +Copy Link

Article reprint please specify:The detailed steps of the.Net Core connection to the MongoDB database | Script Home

You may also be interested in these articles!