/// <summary>
 /// Create a new AdCachedUserProperty object.
 /// </summary>
 /// <param name="userName">Initial value of the UserName property.</param>
 /// <param name="propertyName">Initial value of the PropertyName property.</param>
 /// <param name="isActiveInAd">Initial value of the IsActiveInAd property.</param>
 public static AdCachedUserProperty CreateAdCachedUserProperty(global::System.String userName, global::System.String propertyName, global::System.Boolean isActiveInAd)
 {
     AdCachedUserProperty adCachedUserProperty = new AdCachedUserProperty();
     adCachedUserProperty.UserName = userName;
     adCachedUserProperty.PropertyName = propertyName;
     adCachedUserProperty.IsActiveInAd = isActiveInAd;
     return adCachedUserProperty;
 }
        /// <summary>
        /// Gets the user property from the SQL store.
        /// </summary>
        /// <remarks>
        /// If the SQL store doesn't contain data for the user/property requested it will
        /// add the user/property to the table so it gets fetched next time the system 
        /// refreshes from AD. In the meantime though, the method will return null.
        /// </remarks>
        /// <param name="userName">Name of the user.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The property stored in the SQL cache; null if no value cached</returns>
        internal string GetUserProperty(string userName, string propertyName)
        {
            userName = userName.ToLowerInvariant();
            string propertyValue = null;

            using (TicketDeskEntities ctx = new TicketDeskEntities())
            {
                var val = ctx.AdCachedUserProperties.FirstOrDefault(p => p.UserName == userName && p.PropertyName == propertyName);

                if (val == null)
                {
                    //add this property/user to the table for the next automated refresh
                    var newUserProp = new AdCachedUserProperty()
                                        {
                                            UserName = userName,
                                            PropertyName = propertyName,
                                            PropertyValue = null,
                                            LastRefreshed = null,
                                            IsActiveInAd = true
                                        };
                    ctx.AdCachedUserProperties.AddObject(newUserProp);
                }
                else
                {
                    propertyValue = val.PropertyValue;
                }
                ctx.SaveChanges();
            }

            return propertyValue;
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the AdCachedUserProperties EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAdCachedUserProperties(AdCachedUserProperty adCachedUserProperty)
 {
     base.AddObject("AdCachedUserProperties", adCachedUserProperty);
 }
        private void EnsureStandardPropertiesForAllKnownUsers()
        {
            var allKnownUsers = GetAllKnownDistinctUserNamesFrom();
            using (TicketDeskEntities ctx = new TicketDeskEntities())
            {
                //go ahead and fetch entire table. The table is kinda big, but realistically 
                //  there should only be two rows per valid user... even with a thousand users, 
                //  that's not an obsurde amount of data. 
                var existingUserProperties = ctx.AdCachedUserProperties.ToList();


                //loop 1 to add all display name properties for any users not already in the SQL cache

                var existingProps = existingUserProperties.Where(ep => ep.PropertyName == "displayName");

                foreach (var knownUser in allKnownUsers)
                {
                    if (existingProps.Count(ep => string.Equals(ep.UserName, knownUser, StringComparison.InvariantCultureIgnoreCase)) < 1)
                    {
                        var newUserProp = new AdCachedUserProperty()
                                            {
                                                UserName = knownUser,
                                                PropertyName = "displayName",
                                                IsActiveInAd = true
                                            };
                        ctx.AdCachedUserProperties.AddObject(newUserProp);
                    }
                }

                //loop 2 to add all email properties for any users not already in the SQL cache
                existingProps = existingUserProperties.Where(ep => ep.PropertyName == "mail");

                foreach (var knownUser in allKnownUsers)
                {
                    if (existingProps.Count(ep => string.Equals(ep.UserName, knownUser, StringComparison.InvariantCultureIgnoreCase)) < 1)
                    {
                        var newUserProp = new AdCachedUserProperty()
                                            {
                                                UserName = knownUser,
                                                PropertyName = "mail",
                                                IsActiveInAd = true
                                            };
                        ctx.AdCachedUserProperties.AddObject(newUserProp);
                    }


                   
                }
                ctx.SaveChanges();
            }
        }