Base solution for your next web application
Open Closed

_dbcontext.Users() returns nothing for tenant database #5089


User avatar
0
OriAssurant created

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)
  • User Avatar
    0
    aaron created
    Support Team

    Try:

    var user1 = _context.Users.IgnoreQueryFilters().Any(u => u.UserName = "");
    
  • User Avatar
    0
    OriAssurant created

    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?)

  • User Avatar
    0
    aaron created
    Support Team

    Can you show your Configuration.cs file?

  • User Avatar
    0
    OriAssurant created

    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();  
            }
        }
    }
    
  • User Avatar
    1
    OriAssurant created

    Thanks for your help, we figured it out:

    _context.DisableAllFilters();

  • User Avatar
    0
    aaron created
    Support Team

    That's great :)