I have been working on a .Net Core project and I want to use MongoDb as a database implementation. I have created my entities that will be created as Collections. You can see the entities below.
When I start the project and if the database does not exist on the server, database and collections must be created according to entities that I have created. I should not create a database manually.
For Mssql, I am using FluentMigrator for this process. How can I handle it for MongoDb?
/// <summary>
/// BaseEntity abstract class implementations
/// </summary>
public abstract class BaseEntity : IEntity<string>
{
[BsonRepresentation(BsonType.ObjectId)]
[BsonId]
[BsonElement(Order = 0)]
public string Id { get; } = ObjectId.GenerateNewId().ToString();
[BsonRepresentation(BsonType.DateTime)]
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
[BsonElement(Order = 101)]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
public class Basket : BaseEntity
{
public long CustomerId { get; set; }
public ICollection<BasketItem> BasketItems { get; set; }
}