I separate host and tenant databases and inherit AbpDbContext for tenant databases.
When seeding data, I'm using var user1 = _dbcontext.Users().Any(u=>u.UserName = ***) to identify if a user exists.
But I could not get any result from _dbcontext.Users().Any() even though there are records in the database. This causes duplicate data to be seeded if I run migrator multiple times.
I've tried set current unitofwork to either disable tenant filter or settenant to the target tenant's Id but still couldn't make it work:
_context.CurrentUnitOfWorkProvider.Current.DisableFilter(AbpDataFilters.MayHaveTenant);
_context.CurrentUnitOfWorkProvider.Current.Settenant(2);
Any idea why the normal Linq query doesn't work in this case?
Thank you,
6 Answer(s)
-
0
Try:
var user1 = _context.Users.IgnoreQueryFilters().Any(u => u.UserName = "");
-
0
Hmm.. sorry forgot to mention I'm using MVC5.* + AngularJS. Seems IgnoreQueryFilters is not available in this version? I got this error:
IDbSet<User>' does not contain a definition for 'IgnoreQueryFilters' and no extension method 'IgnoreQueryFilters' accepting a first argument of type 'IDbSet<User>' could be found (are you missing a using directive or an assembly reference?)
-
0
Can you show your Configuration.cs file?
-
0
Sure, thank you Aaron. Here is the configuration.cs for tenant databases:
namespace x.y.PortalDBOneMigrations { public sealed class Configuration : DbMigrationsConfiguration<EntityFramework.PortalDbOneContext>, IMultiTenantSeed { public AbpTenantBase Tenant { get; set; } public Configuration() { } protected override void Seed(PortalDbOneContext context) { context.EntityChangeEventHelper = NullEntityChangeEventHelper.Instance; context.EventBus = NullEventBus.Instance; //seeding data for tenant id 2 new UserRolePermissionTenantCreator(context, 2).CreateUserRolePermission(); } } }
-
1
Thanks for your help, we figured it out:
_context.DisableAllFilters();
-
0
That's great :)