C #/. Net Core access the MongoDb database,

Source: Internet
Author: User

C #/. Net Core access the MongoDb database,

Let's talk about Code directly.

Connection string:

{"Etettings": {"mongodb": "mongodb: // username: password @ IP Address: Port Number "}}

Subject code:

1 using ABCDEFG. config; 2 using MongoDB. driver; 3 using System; 4 using System. collections. generic; 5 using System. linq. expressions; 6 using System. text; 7 8 namespace Mongodb 9 {10 public class extends context11 {12 public encryption context () 13 {14 Client = new Consumer Client (ABCDEFG .. getdeleetting ("mongodb"); 15} 16 17 public encryption context (string connectionName) 18 {19 Client = new Login Client (MengTConfig. getAppSetting (connectionName); 20} 21 22 private login Client {get; set;} 23 24 private IMongoDatabase DataBase {get => Client. getDatabase ("MengTLog");} 25 26 public IMongoCollection <T> DbSet <T> () where T: IMongoModel => DataBase. getCollection <T> ("MengTLog. logger "); 27 28} 29 30 public static class extends extend31 {32 public static void Add <T> (this IMongoCollection <T> collenction, T Model) where T: IMongoModel33 => collenction. insertOne (Model); 34 35 public static void AddList <T> (this IMongoCollection <T> collenction, List <T> Model) where T: IMongoModel36 => collenction. insertmodel (Model ); 37 38 // <summary> 39 // find the first 40 /// </summary> 41 public static T FirstOrDefault <T> (this IMongoCollection <T> collenction, expression <Func <T, bool> expression) where T: IMongoModel42 {43 if (expression = null) {throw new ArgumentNullException ("invalid parameter");} 44 return collenction. find (expression ). firstOrDefault (); 45} 46 47 // <summary> 48 // find the compliant data List 49 // </summary> 50 public static List <T> FindToList <T> (this IMongoCollection <T> collenction, expression <Func <T, bool> expression) where T: IMongoModel51 {52 if (expression = null) {throw new ArgumentNullException ("invalid parameter");} 53 return collenction. find (expression ). toList (); 54} 55 56 // <summary> 57 // Delete all matched data 58 // </summary> 59 public static void Delete <T> (this IMongoCollection <T> collenction, expression <Func <T, bool> expression) where T: IMongoModel60 {61 if (expression = null) {throw new ArgumentNullException ("invalid parameter");} 62 collenction. deleteManyAsync (expression ); 63} 64 65 66 // <summary> 67 // delete a 68 // </summary> 69 public static void DeleteOne <T> (this IMongoCollection <T> collenction, expression <Func <T, bool> expression) where T: IMongoModel70 {71 if (expression = null) {throw new ArgumentNullException ("invalid parameter");} 72 collenction. deleteOneAsync (expression); 73} 74} 75}

IMongoModel:

1 using MongoDB. bson; 2 using System; 3 using System. collections. generic; 4 using System. text; 5 6 namespace Mongodb 7 {8 public partial class IMongoModel 9 {10 // <summary> 11 // basic ID12 /// </summary> 13 public ObjectId _ id {get; set ;}14} 15}


It is worth noting that MongoDB. BSon and MongoDB. Driver must be referenced.

If the namespace still cannot be found after the package is referenced in vs7 7, restart.

ABCDEFG. getdeleetting ("mongodb"); read the configuration file

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.