Browse Source

Update document

pull/19073/head
liangshiwei 3 years ago
parent
commit
23b80f05b4
  1. 28
      docs/en/MongoDB.md
  2. 28
      docs/zh-Hans/MongoDB.md

28
docs/en/MongoDB.md

@ -82,6 +82,34 @@ If you only need to configure the collection name, you can also use `[MongoColle
public IMongoCollection<Question> Questions => Collection<Question>();
````
### Configure Indexes and CreateCollectionOptions for a Collection
You can configure indexes and `CreateCollectionOptions` for your collections by overriding the `CreateModel` method. Example:
````csharp
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
base.CreateModel(modelBuilder);
modelBuilder.Entity<Question>(b =>
{
b.CreateCollectionOptions.Collation = new Collation(locale:"en_US", strength: CollationStrength.Secondary);
b.ConfigureIndexes(indexes =>
{
indexes.CreateOne(
new CreateIndexModel<BsonDocument>(
Builders<BsonDocument>.IndexKeys.Ascending("MyProperty"),
new CreateIndexOptions { Unique = true }
)
);
}
);
});
}
````
This example sets a collation for the collection and creates a unique index for the `MyProperty` property.
### Configure the Connection String Selection
If you have multiple databases in your application, you can configure the connection string name for your DbContext using the `[ConnectionStringName]` attribute. Example:

28
docs/zh-Hans/MongoDB.md

@ -51,6 +51,34 @@ public class MyDbContext : AbpMongoDbContext
* 为每一个mongo集合添加一个公共的 `IMongoCollection<TEntity>` 属性.ABP默认使用这些属性创建默认的仓储
* 重写 `CreateModel` 方法,可以在方法中配置集合(如设置集合在数据库中的名字)
### 为集合配置索引和 CreateCollectionOptions
你可以在 `CreateModel` 方法中配置集合的索引和 `CreateCollectionOptions`:
````csharp
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
base.CreateModel(modelBuilder);
modelBuilder.Entity<Question>(b =>
{
b.CreateCollectionOptions.Collation = new Collation(locale:"en_US", strength: CollationStrength.Secondary);
b.ConfigureIndexes(indexes =>
{
indexes.CreateOne(
new CreateIndexModel<BsonDocument>(
Builders<BsonDocument>.IndexKeys.Ascending("MyProperty"),
new CreateIndexOptions { Unique = true }
)
);
}
);
});
}
````
在这个例子中,我们设置了集合的排序规则和一个唯一的索引.
### 将 Db Context 注入到依赖注入中
在你的模块中使用 `AddAbpDbContext` 方法将Db Context注入到[依赖注入](Dependency-Injection.md)系统中.

Loading…
Cancel
Save