Well,
//Seed initial data for default tenant
AbpSession.TenantId = 1;
UsingDbContext(context =>
{
new TenantRoleAndUserBuilder(context, AbpSession.GetTenantId()).Create();
new DefaultTenantBusinessDataCreator(context, AbpSession.GetTenantId()).Create(20); // Gp - create app business data
});
It is you original code. My code is just the last line (// Gp ...)
TenantId is correctly 1, when I extract it from DB:
PropertyForSale pfs = null;
await UsingDbContextAsync(async context => {
pfs = await context.REProperties.OfType<PropertyForSale>()
.Include(p => p.Owner)
.Include(p => p.Solicitor)
.Include(p => p.ZipCode)
.Include(p => p.City)
.Include(p => p.Attachments)
.Include(p => p.SaleAgreement)
.FirstAsync();
});
But when I execute the line
_myRepository.FirstOrDefaultAsync(p => p.Id == pfsId)
or
AbpSession.TenantId = 1;
. . .
_myRepository.FirstOrDefaultAsync(p => p.Id == pfsId)
it returns nothing.
even the code _myRepository.GetAllList() returns an empty list.
This is the same issue I have with tenant data.
All my tests running on non-tenant data works, but when I call the AppService working on tenant data it always return 0.
I tried to disable filters, set the correct tenant Id without success.
This is the test
public async Task GetShowCase_Test() {
// I call the DB just to be sure the query returns the correct # of items (16)
IList<PropertyForSale> dbShowCase = null;
await UsingDbContextAsync(async context => {
dbShowCase = await context.REProperties.OfType<PropertyForSale>()
.Include(p => p.SaleAgreement)
.Where(p => p.SaleAgreement == null)
.ToListAsync();
});
var input = new GetPfsShowCaseInput {
MaxResultCount = 100,
SkipCount = 0
};
var output = await _pfsAppService.GetShowCase(input);
output.Items.Count.ShouldBeGreaterThan(0); // this fails as items are 0!
}
This is the service method that works when I run it normally
public async Task<GetPfsShowCaseOutput> GetShowCase(GetPfsShowCaseInput input) {
// this returns 0 tiems during unit test
var items = _propertiesManager.GetShowCase(input.SkipCount, input.MaxResultCount, input.Sorting);
return new GetPfsShowCaseOutput {
Items = items.MapTo<ReadOnlyCollection<PropertyForSaleDto>>()
};
}
}
And this is the Repository code:
public IQueryable<PropertyForSale> GetShowCase(int skipCount, int maxResultCount, string sorting = null) {
var query = GetAll()
.Where(p => (p.ShowOnline && (p.SaleAgreement == null)));
// sorting and paging
query = query
.OrderByDescending(p => p.ShowOnlineDate);
query = query
.Skip(skipCount)
.Take(maxResultCount);
return query;
}
I try to reply by myself hoping some feedback about my solution.
[DependsOn(typeof(MyProjectCoreModule), typeof(AbpAutoMapperModule))]
public class MyProjectApplicationModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
// --- MY CODE
var mapperConfiguration = new MapperConfiguration(cfg => {
cfg.AddProfile(new MyProjectMapperProfile()); // <= here my custom mapping
});
var mapper = mapperConfiguration.CreateMapper();
IocManager.IocContainer.Register(
Castle.MicroKernel.Registration.Component.For<IMapper>().Instance(mapper)
);
// --- MY CODE
}
}
public class UserAppService : MyNewHouseAppServiceBase, IUserAppService {
. . .
public UserAppService(IRepository<User, long> userRepository, AutoMapper.IMapper mapper) {
. . .
}
public async Task<ListResultOutput<UserListDto>> GetUsers() {
var users = await _userRepository.GetAllListAsync();
return new ListResultOutput<UserListDto>(
// USE THE INJECTED MAPPER
_mapper.Map<List<UserListDto>>(users)
);
}
}
What do you think about this solution?
Tnx gp