public static era_jurisdiction?LookupJurisdictionByCode(this EssContext context, string code) { if (string.IsNullOrEmpty(code) || !Guid.TryParse(code, out var parsedCode)) { return(null); } return(context.era_jurisdictions_cached.Value.Where(p => p.era_jurisdictionid == parsedCode).SingleOrDefault()); }
public static era_provinceterritories?LookupStateProvinceByCode(this EssContext context, string code) { if (string.IsNullOrEmpty(code)) { return(null); } return(context.era_provinceterritories_cached.Value.Where(p => p.era_code == code).SingleOrDefault()); }
public static era_country?LookupCountryByCode(this EssContext context, string code) { if (string.IsNullOrEmpty(code)) { return(null); } return(context.era_countries_cached.Value.Where(p => p.era_countrycode == code).SingleOrDefault()); }
public static era_country LookupCountryByCode(this EssContext context, string code) { if (string.IsNullOrEmpty(code)) { return(null); } return(context.era_countries.Where(p => p.era_countrycode == code).FirstOrDefault()); }
public static void DetachAll(this EssContext context) { foreach (var descriptor in context.EntityTracker.Entities) { context.Detach(descriptor.Entity); } foreach (var link in context.EntityTracker.Links) { context.DetachLink(link.Source, link.SourceProperty, link.Target); } }
public static async Task <T> SaveChangesAsync <T>(this EssContext context, SaveChangesOptions?options = null) where T : BaseEntityType { var response = await context.SaveChangesAsync(options ?? SaveChangesOptions.BatchWithSingleChangeset); //TODO: handle errors properly var change = response.First() as ChangeOperationResponse; var descriptor = change.Descriptor as EntityDescriptor; return(descriptor.Entity as T); }
private static void ModifyEntityStatus(this EssContext context, object entity, EntityStatus status) { var entityType = entity.GetType(); if (!typeof(crmbaseentity).IsAssignableFrom(entityType)) { throw new InvalidOperationException($"entity {entityType.FullName} is not a valid {typeof(crmbaseentity).FullName}"); } var statusProp = entity.GetType().GetProperty("statuscode"); var stateProp = entity.GetType().GetProperty("statecode"); statusProp.SetValue(entity, (int)status); stateProp.SetValue(entity, (int)MapStatusToState(status)); context.UpdateObject(entity); }
/// <summary> /// Check if the entity is already tracked by the context, if no, then attach and return, if yes, return the tracked entity instead /// </summary> /// <typeparam name="TEntity">entity type</typeparam> /// <param name="context">Dynamics context</param> /// <param name="entitySetName">name of the entity set</param> /// <param name="entity">the entity to attach</param> /// <param name="entityKeyGetter"> a function to resolve the entity id</param> /// <returns>a tracked entity - either existing or newly attached</returns> public static TEntity AttachOrGetTracked <TEntity>(this EssContext context, string entitySetName, TEntity entity, Func <TEntity, Guid?> entityKeyGetter) where TEntity : crmbaseentity { var currentEntityKey = entityKeyGetter(entity); if (!currentEntityKey.HasValue) { return(entity); } var matchedEntity = context.EntityTracker.Entities .Where(ed => ed.Entity is TEntity && entityKeyGetter((TEntity)ed.Entity) == currentEntityKey) .SingleOrDefault()?.Entity as TEntity; if (matchedEntity == null) { context.AttachTo(entitySetName, entity); } return(matchedEntity ?? entity); }
public static void DeactivateObject(this EssContext context, object entity, int inactiveStatusValue = -1) => ModifyEntityStatus(context, entity, (int)EntityState.Inactive, inactiveStatusValue);
public static void DeactivateObject(this EssContext context, object entity) => ModifyEntityStatus(context, entity, EntityStatus.Inactive);
public static void SoftDeleteObject(this EssContext context, object entity) => ModifyEntityStatus(context, entity, EntityStatus.SoftDelete);
public static async Task <systemuser> GetCurrentSystemUser(this EssContext ctx) { var currentUserId = (await ctx.WhoAmI().GetValueAsync()).UserId; return(await ctx.systemusers.Where(su => su.systemuserid == currentUserId).SingleOrDefaultAsync()); }