标签:family tps asp https string dll size log style
"没有时间了!"
本次学习mongoDB,参考了如下文章:
Using MongoDB with ASP.NET Core – Part II (Implementation)
来自 <https://www.janaks.com.np/using-mongodb-with-aspnet-core-ii/>
MongoDB学习笔记(二) 通过samus驱动实现基本数据操作
来自 <http://www.cnblogs.com/lipan/archive/2011/03/08/1977691.html>
略去如何安装和调试MongoBD,可参见上述两个文章。
在进行下述操作之前,请先确定MongoDB服务已经开启,不知道怎么开启服务,请看MongoDB的安装和配置。
下载驱动,新建控制台项目(.net Core)
并添加对MongoDB.dll的引用,nuget 搜MongoDB(当前版本2.4.3)。
private IMongoDatabase _database; private string _connStr = "mongodb://localhost:27017"; private string _dbName = "VesselDB"; |
//创建数据库代理类,链接数据库
public VesselRepository() { var client = new MongoClient(_connStr); _database = client.GetDatabase(_dbName); } |
//定义一个对象
public class Vessel { [BsonId] public int IMO { get; set; } [BsonElement("VesselName")] public string Name { get; set; } [BsonElement("PortOfRegistry")] public string Registry { get; set; } [BsonElement("YearofBuilt")] public DateTime YearOfBuilt { get; set; } } |
//增改删查方法
// AddMethod public void AddVessel(Vessel vsl) { _database.GetCollection<Vessel>("Vessels").InsertOne(vsl); } // DeletedMethod
public void DeleteVessel(string vslName) { var filter = Builders<Vessel>.Filter.Eq(vsl => vsl.Name, vslName); _database.GetCollection<Vessel>("Vessels").DeleteOne(filter); }
// GetMentod public IEnumerable<Vessel> GetAllVessels() { return _database.GetCollection<Vessel>("Vessels").Find(FilterDefinition<Vessel>.Empty).ToList(); }
// UpdateMethod public void UpdateVessel(int imo, Vessel vsl) { var filter = Builders<Vessel>.Filter.Eq(v => v.IMO, imo); var update = Builders<Vessel>.Update .Set(v => v.Name, vsl.Name) .Set(v => v.Registry, vsl.Registry) .Set(v => v.YearOfBuilt, vsl.YearOfBuilt); _database.GetCollection<Vessel>("Vessels").UpdateOne(filter, update); } |
完美运行起来了。
Asp Net Core Fro MongoDB Part1
标签:family tps asp https string dll size log style
原文地址:http://www.cnblogs.com/qicai21/p/6823075.html