標籤: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