How to get current Catalog Name

Posts   
 
    
Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 13-Oct-2009 15:15:17   

Simply said: When I call code CustomerEntity customer = new CustomerEntity(); I want to get the used catalog name.

I use sqlServerCatalogNameOverwrites and my solution includes more llbl projects. At design time I don't know what catalogname was used to generate llbl.

using v2.6

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 13-Oct-2009 18:18:20   

Can't you read the CatalogNameToUse property from the active adapter ?

Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 13-Oct-2009 18:51:16   

How to get the current adapter. I don't see it. I'm using SelfServicing template group, not Adapter

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 13-Oct-2009 22:35:18   

All persistence info is generated into the PersistenceInfoProvider class which is in the DBSpecificProject in adapter and in the HelperClasses folder in selfservicing.

You should be able to read it from there...

Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 14-Oct-2009 10:07:35   

In adapter, there's a protected method called GetFieldPersistenceInfos(entity). Add a method to a partial class of the generated DataAccessAdapter and call the GetFieldPersistenceInfos method from that method, and with those persistence infos you can obtain the catalog name, as it's in the field elements simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 14-Oct-2009 14:23:06   

here's what i've got: frowning


  public partial class DbUtils
  {
    public static string GetCurrentCatalogName()
    {
      string catalogName = OriginalCatalogName;
      Dictionary<string, string> catalogNames = getOverwritesDictionary();
      if (catalogNames.ContainsKey(OriginalCatalogName))
      {
        catalogName = catalogNames[OriginalCatalogName];
        if (string.IsNullOrEmpty(catalogName)) return OriginalCatalogName;
      }
      return catalogName; 
    }

    public static string OriginalCatalogName
    {
      get {
        if (_originalCatalogName == null)
        {
         _originalCatalogName = PersistenceInfoProviderSingleton.GetInstance().GetFieldPersistenceInfo("CustomerEntity", "IdCustomer").SourceCatalogName;
        }
        return _originalCatalogName;
      }
    }

    private static string _originalCatalogName;
    

    private static Dictionary<string, string> getOverwritesDictionary()
    {
      var obj = typeof(DynamicQueryEngine).InvokeMember("_catalogOverwrites", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Static, null, null, null);
      return obj as Dictionary<string, string>;
    }

  }

Isn't there better way?

edit there's also bug in code i posted. If catalogowerwrite owerwrites catalog name to an empty string it uses catalog from connection string, not original catalog

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 14-Oct-2009 17:19:12   

If you overwirte the catalog name using the config file, then the code you have posted should get you the current used catalog name from the CatalogNameOverwrites hashtable.

If you are using an empty overwrite, then you should examine the DBUtils.ActualConnectionString instead.

Otherwise if you don't use catalog name overwrites then grab the SourceCatalogName of any entityField.

var myCustomer = new CustomerEntity("ALFKI"); var catalogName = myCstomer.Fields[0].SourceCatalogName;