using MongoDB.Bson.Serialization.Attributes;using System.Collections.Generic;
namespace MongoCore.Models
{
public class Province
{
[BsonId]
public int ProvinceID { get; set; }
public string ProvinceName { get; set; }
///
/// 省份里有多个学校 这里用集合保存
///
public IList SchoolName { get; set; }
}
}
namespace MongoCore.Models
{ //用于后面添加学校 public School(string schoolName, string years) { SchoolName = schoolName; Years = years; }
public class School
{
public string SchoolName { get; set; }
public string Years { get; set; }
}
}
创建上下文类,连接MongoDB
namespace MongoCore.Models
{
public class ProvinceContext
{
//定义数据库
private readonly IMongoDatabase _database = null;
public ProvinceContext()
{
//连接服务器名称 mongo的默认端口27017
var client = new MongoClient("mongodb://.......:27017");
if (client != null)
//连接数据库
_database = client.GetDatabase("数据库名");
}
public IMongoCollection Province
{
get
{
return _database.GetCollection("Province");
}
}
}
}
创建控制器
private readonly ProvinceContext _cOntext= new ProvinceContext();
public async Task Index()
{
var list = await _context.Province.Find(_ => true).ToListAsync();
return View(list);
}
Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ...
[详细]
This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ...
[详细]