====== Laden von Daten aus einer DB ====== Beispielcode für: * **Map.Functions.CountryCode('DE')**, lädt den country code aus der lokalen DB von einer benutzerdefinierten Tabelle. * **Map.Functions.CountryCodeFromDb('DE')**, lädt den country code aus einer Tabelle von einer anderen DB. Referenzierte Assemblies: eBiss.Api.dll und Pranke.Orm.TypeLib.dll using eBiss.Api; using eBiss.Api.Data; using Pranke.Attributes; using System.Linq; using Pranke.Orm.Interfaces; using System.Collections; namespace Map { [OrmClass(tableName: "my_country_codes")] public abstract class CountryCodes { [Key] public abstract string CountryCode { get; set; } public abstract string Description { get; set; } } /// /// /// public class Functions : IMapFunctionExtender, ILoggingObject, IApplicationConsumer, ISystemVariableConsumer { private static string Db_Connection_Variable = "Db.CountryCodes.Connection"; public IApplication Application { get; set; } public ILogContext Log { get; set; } public IDictionary Variables { get; set; } /// /// Select from my_country_codes using local DB /// /// /// /// Sample Call: Map.Functions.CountryCode('DE') [eBiss.Api.MappingFunction] public object CountryCode(string code) { CountryCodes foundItem = Application.Entities.DbSet().Where(c => c.CountryCode == code).FirstOrDefault(); return foundItem?.Description; } /// /// Select from my_country_codes using from diffrent DB, in this sample from MS-SQLServer DB eBiss3Demo /// /// /// /// Sample Call: Map.Functions.CountryCodeFromDb('DE') [eBiss.Api.MappingFunction] public object CountryCodeFromDb(string code) { Pranke.Orm.Interfaces.IEntities entities = Pranke.Orm.EntitiesBase.Connect(Pranke.Orm.DatabaseType.MsSql, GetDbConnection()); IQueryResult res = entities.DbSet(); res.Query.Timeout = 100; CountryCodes foundItem = res.Where(c => c.CountryCode == code).FirstOrDefault(); return foundItem?.Description; } /// /// the Connection string has the same format as the one in the eBiss.Config under 'add key="DbConnectionString"...', /// e.g. with „…;uid=;pwd=“ and not "User=; Password=" /// /// /// private string GetDbConnection() { if (Variables.Contains(Db_Connection_Variable) && string.IsNullOrWhiteSpace(Variables[Db_Connection_Variable].ToString()) == false) { return Variables[Db_Connection_Variable].ToString(); } else { throw new System.ApplicationException($"Missing Variable {Db_Connection_Variable}!"); } } } }