1 Star 7 Fork 0

code-Institutes/Apteryx.MongoDB.Driver.Extend

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

针对MongoDB.Driver的扩展

安装

Install-Package Apteryx.MongoDB.Driver.Extend

使用方法一:

public class Account:BaseMongoEntity
{
    public string Name { get; set; }
    public string Mobile { get; set; }
}

public class MyDbService:MongoDbService
{
    public MyDbService(IOptionsMonitor<MongoDBOptions> options) : base(options){}
    public IMongoCollection<Account> Account => _database.GetCollection<Account>("Account");
}

public class Startup
{
  public Startup(IConfiguration configuration)
  {
    Configuration = configuration;
  }

  public IConfiguration Configuration { get; }

  public void ConfigureServices(IServiceCollection services)
  {
    services.AddMongoDB<MyDbService>(options =>
    {
      options.ConnectionString = Configuration.GetConnectionString("MongoDbConnection");
    });
    //.....................
  }
}

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private MyDbService db = null;
    public ValuesController(IMongoDbService db)
    {
        this.db = (MyDbService)db;
    }
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        db.Account.Add(new Account(){Name = "张三",Mobile = "13812345678"});
        return new string[] { "value1", "value2" };
    }
}

使用方法二:

public class Account:BaseMongoEntity
{
    public string Name { get; set; }
    public string Mobile { get; set; }
}

public class MyDbService:MongoDbService
{        
    public MyDbService(string conn):base(conn){}
    //public MyDbService(IOptionsMonitor<MongoDBOptions> options) : base(options){}
    public IMongoCollection<Account> Account => _database.GetCollection<Account>("Account");
}

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        var db = new MyDbService("mongodb://user:pwd@xxx.xxx.xxx.xxx:27017/testdb?authSource=admin");
        db.Account.Add(new Account(){Name = "张三",Mobile = "13812345678"});
        return new string[] { "value1", "value2" };
    }
}

空文件

简介

针对MongoDB.Driver的扩展 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/code-Institutes/Apteryx.MongoDB.Driver.Extend.git
git@gitee.com:code-Institutes/Apteryx.MongoDB.Driver.Extend.git
code-Institutes
Apteryx.MongoDB.Driver.Extend
Apteryx.MongoDB.Driver.Extend
master

搜索帮助