Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Add data on Seed depending on feature #1851


User avatar
0
andmattia created

I try to add some static data on Seed method but I want to add row only if a feature is enable. I try but I not find how can get feature from database if i call

var featureSetting in allFeature.Where(t=> t.TenantId == tenant.Id).ToList()

it's always empty


5 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Can you paste the whole code. And be sure you are querying against to the host database, not tenant database, because feature settings are stored in the host database.

  • User Avatar
    0
    andmattia created

    this is the full code

    private void CreateDefaultTemplate()
            {
                var tenants = _context.Tenants.ToList();
                var allFeature = _context.TenantFeatureSettings.ToList();
    
                foreach (var tenant in tenants)
                {
                    
                    foreach (var featureSetting in allFeature.Where(t=> t.TenantId == tenant.Id).ToList() )
                    {
                        
                        foreach (var template in InitialTemplates)
                        {
                            if (featureSetting.Name == template.ApplicationName)
                            {
                                template.TenantId = tenant.Id;
                                AddTemplateIfNotExists(template);
                            }
                           
                        }
                    }    
                }
            }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Your code seems fine. Check this:

    1. Is that code running in the host db (if you are using tenant per db)?
    2. Is _context.TenantFeatureSettings.ToList(); returns the true list?
  • User Avatar
    0
    andmattia created

    I try and the list is empty (0 element).

    i call my method from InitialHostDbBuilder

  • User Avatar
    0
    andmattia created

    I found a solution. I check in Tenant but is setted on edition!

    if(!tenant.EditionId.HasValue)
                        continue;
                    var ed = _context.Editions.FirstOrDefault(t => t.Id == tenant.EditionId.Value);
                    var allFeature = _context.EditionFeatureSettings.Where(t=>t.EditionId == ed.Id).ToList();
    for(....)