Browse Source

Translate the MongoDB.md

pull/536/head
edison 8 years ago
parent
commit
ad413d19da
  1. 33
      docs/zh-Hans/MongoDB.md

33
docs/zh-Hans/MongoDB.md

@ -1,16 +1,16 @@
## MongoDB Integration ## MongoDB 集成
This document explains how to integrate MongoDB as a database provider to ABP based applications and how to configure it. 本文会介绍如何将MongoDB集成到基于ABP的应用程序中以及如何配置它
### Installation ### 安装
`Volo.Abp.MongoDB` is the main nuget package for the MongoDB integration. Install it to your project (for a layered application, to your data/infrastructure layer): 集成MongoDB需要用到`Volo.Abp.MongoDB`这个包。将它安装到你的项目中(如果是多层架构,安装到数据层和基础设施层):
``` ```
Install-Package Volo.Abp.MongoDB Install-Package Volo.Abp.MongoDB
``` ```
Then add `AbpMongoDbModule` module dependency to your [module](Module-Development-Basics.md): 然后添加 `AbpMongoDbModule` 依赖到你的 [模块](Module-Development-Basics.md)中:
```c# ```c#
using Volo.Abp.MongoDB; using Volo.Abp.MongoDB;
@ -26,9 +26,9 @@ namespace MyCompany.MyProject
} }
``` ```
### Creating a Mongo Db Context ### 创建一个Mongo Db Context
ABP introduces **Mongo Db Context** concept (which is similar to Entity Framework Core's DbContext) to make it easier to use collections and configure them. An example is shown below: ABP中引入了 **Mongo Db Context** 的概念(跟Entity Framework Core的DbContext很像)让使用和配置集合变得更简单。举个例子:
```c# ```c#
public class MyDbContext : AbpMongoDbContext public class MyDbContext : AbpMongoDbContext
@ -47,13 +47,13 @@ public class MyDbContext : AbpMongoDbContext
} }
``` ```
* It's derived from `AbpMongoDbContext` class. * 继承 `AbpMongoDbContext` 类
* Adds a public `IMongoCollection<TEntity>` property for each mongo collection. ABP uses these properties to create default repositories by default. * 为每一个mongo集合添加一个公共的 `IMongoCollection<TEntity>` 属性。ABP默认使用这些属性创建默认的仓储
* Overriding `CreateModel` method allows to configure collections (like their collection name in the database). * 重写 `CreateModel` 方法,可以在方法中配置集合(如设置集合在数据库中的名字)
### Registering DbContext To Dependency Injection ### 将 Db Context 注入到依赖注入中
Use `AddAbpDbContext` method in your module to register your DbContext class for [dependency injection](Dependency-Injection.md) system. 在你的模块中使用 `AddAbpDbContext` 方法将Db Context注入到 [依赖注入](Dependency-Injection.md)系统中.
```c# ```c#
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -75,9 +75,9 @@ namespace MyCompany.MyProject
} }
``` ```
#### Add Default Repositories #### 添加默认的仓储
ABP can automatically create [repositories](Repositories.md) for the entities in your Db Context. Just use `AddDefaultRepositories()` option on registration: 在注入的时候使用 `AddDefaultRepositories()`, ABP就能自动为Db Context中的每一个实体创建 [仓储](Repositories.md):
````C# ````C#
services.AddMongoDbContext<MyDbContext>(options => services.AddMongoDbContext<MyDbContext>(options =>
@ -86,7 +86,7 @@ services.AddMongoDbContext<MyDbContext>(options =>
}); });
```` ````
This will create a repository for each aggreate root entity (classes derived from AggregateRoot) by default. If you want to create repositories for other entities too, then set `includeAllEntities` to `true`: 这样就会默认为每一个聚合根实体(继承自AggregateRoot的类)创建一个仓储。如果你也想为其他的实体创建仓储,将 `includeAllEntities` 设置为 `true`就可以了:
```c# ```c#
services.AddMongoDbContext<MyDbContext>(options => services.AddMongoDbContext<MyDbContext>(options =>
@ -95,5 +95,4 @@ services.AddMongoDbContext<MyDbContext>(options =>
}); });
``` ```
Then you can inject and use `IRepository<TEntity>` or `IQueryableRepository<TEntity>` in your services. 现在可以在你的服务中注入并使用`IRepository<TEntity>` 或 `IQueryableRepository<TEntity>`了。
Loading…
Cancel
Save