Пример #1
0
        public static T CreateInMemoryEFContext <T>(Func <EFContextOptions, T> efContectFactory, Guid?systemUserId = null)
            where T : EFContext
        {
            var modelBuilder = new ModelBuilder(new Microsoft.EntityFrameworkCore.Metadata.Conventions.ConventionSet());

            EFContext.FixSnakeCaseNames(modelBuilder);
            EFMappings.DefineMappings(modelBuilder);

            var dbContextOptions = new DbContextOptionsBuilder <T>()
                                   .UseInMemoryDatabase("costs")
                                   .UseModel(modelBuilder.Model)
                                   .ConfigureWarnings(wcb => wcb.Ignore(InMemoryEventId.TransactionIgnoredWarning))
                                   .Options;

            var efContextOptions = new EFContextOptions
            {
                DbContextOptions = dbContextOptions,
                SystemUserId     = systemUserId ?? Guid.Empty
            };

            return(efContectFactory(efContextOptions));
        }