1. 程式人生 > >C#封裝MongoDb.Driver 實現EF操作SqlServer模式

C#封裝MongoDb.Driver 實現EF操作SqlServer模式

1.封裝

    public class UserDataContext
    {
        private IMongoDatabase _mongoDatabase;

        public UserDataContext()
        {
            MongoUrl mongourl = new MongoUrl("mongodb://cy:[email protected]:27017/User");
          //  MongoUrl mongourl = new MongoUrl(ConfiguredManager.AppSettings["MongoDb"]);
var mongoClient = new MongoClient(mongourl); _mongoDatabase = mongoClient.GetDatabase(mongourl.DatabaseName); } public IMongoCollection<Post> Post => _mongoDatabase.GetCollection<Post>(typeof (Post).Name); }

2.使用

  var p = new UserDataContext().Post
.AsQueryable().Count();

完畢,可再優化請指教