From 7c36f720162370b0bba70d72d5288ab526939987 Mon Sep 17 00:00:00 2001 From: Joshua Davidson Date: Thu, 27 Sep 2018 15:05:04 -0500 Subject: [PATCH] Add collection naming & ObjectId documentation Document collection naming scheme and ability to rename collection with `CollectionName` attribute. Document optional use of `MongoDB.Bson.ObjectId` for entity keys. Update headers for clarity. --- README.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 66b8aa6..6a6a473 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,7 @@ Available as a Nuget package : https://www.nuget.org/packages/AspNetCore.Identit Install-Package AspNetCore.Identity.MongoDbCore -# Usage examples - +# User and Role Entities Your user and role entities must inherit from `MongoIdentityUser` and `MongoIdentityRole` in a way similar to the `IdentityUser` and the `IdentityRole` in `Microsoft.AspNetCore.Identity`, where `TKey` is the type of the primary key of your document. Here is an example: @@ -41,8 +40,23 @@ public class ApplicationRole : MongoIdentityRole } } ``` -The `Id` field is automatically set at instantiation, this also applies to users inheriting from `MongoIdentityUser`, where a random integer is assigned to the `Id`. It is however not advised to rely on such random mechanism to set the primary key of your document. Using documents inheriting from `MongoIdentityRole` and `MongoIdentityUser`, which both use the `Guid` type for primary keys, is recommended. +#### Id Fields +The `Id` field is automatically set at instantiation, this also applies to users inheriting from `MongoIdentityUser`, where a random integer is assigned to the `Id`. It is however not advised to rely on such random mechanism to set the primary key of your document. Using documents inheriting from `MongoIdentityRole` and `MongoIdentityUser`, which both use the `Guid` type for primary keys, is recommended. MongoDB ObjectIds can optionally be used in lieu of GUIDs by passing a key type of `MongoDB.Bson.ObjectId`, e.g. `public class ApplicationUser : MongoIdentityUser`. +#### Collection Names +MongoDB collection names are set to the plural camel case version of the entity class name, e.g. `ApplicationUser` becomes `applicationUsers`. To override this behavior apply the `CollectionName` attribute to MongoDbGenericRepository: +```csharp +using MongoDbGenericRepository.Attributes; + +namespace App.Entities +{ + // Name this collection Users + [CollectionName("Users")] + public class ApplicationUser : MongoIdentityUser + { + ... +``` +# Configuration To add the stores, you can use the `IdentityBuilder` extension like so: ```csharp