The example provided in bogus's github repo makes use of the HasData method of the CatalogDbContext class to seed data into the tables.
However, I will not want this HasData method to be executed from the API - meaning, the HasData method should only be run if the DBContext is created from the Unit Tests.
Kindly advise how to achieve this?.
What possible solutions have you considered?
Do you have sample code to show what you're trying to do?
using Bogus;
using Catalog.Api.Database.Entities;
using Microsoft.EntityFrameworkCore;
void Main()
{
}
namespace Catalog.Api.Database
{
public class CatalogDbContext : DbContext
{
public CatalogDbContext(DbContextOptions<CatalogDbContext> options) : base(options)
{
}
public DbSet<CatalogItem> CatalogItems { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new CatalogItemEntityTypeConfiguration());
FakeData.Init(10);
builder.Entity<CatalogItem>().HasData(FakeData.CatalogItems);
}
}
internal class FakeData
{
public static List<CatalogItem> CatalogItems = new List<CatalogItem>();
public static void Init(int count)
{
var id = 1;
var catalogItemFaker = new Faker<CatalogItem>()
.RuleFor(ci => ci.Id, _ => id++)
.RuleFor(ci => ci.Name, f => f.Commerce.ProductName());
}
}
public class CatalogItem
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Not sure if I understand correctly, but why don't you just use this customized CatalogDbContext for unit testing only, and a non-customized context otherwise?
Version Information
What locale are you using with Bogus?
English
What's the problem?
The example provided in bogus's github repo makes use of the
HasData
method of theCatalogDbContext
class to seed data into the tables.However, I will not want this
HasData
method to be executed from the API - meaning, theHasData
method should only be run if theDBContext
is created from the Unit Tests.Kindly advise how to achieve this?.
What possible solutions have you considered?
Do you have sample code to show what you're trying to do?