public virtual bool ObjectHasReference(object entity)
        {
            if (entity == null)
            {
                return(false);
            }

            IHasLoggingReference entityWithReference = entity as IHasLoggingReference;

            if (entityWithReference != null)
            {
                return(true);
            }

            const BindingFlags flags           = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase;
            string             keyPropertyName = GetReferencePropertyForObject(entity);
            var keyProperty = entity.GetType().GetProperty(keyPropertyName, flags);

            if (keyProperty != null)
            {
                return(true);
            }

            return(false);
        }
示例#2
0
        public virtual string GetReferenceForObject(object entity)
        {
            if (entity == null)
            {
                return(null);
            }

            IHasLoggingReference entityWithReference = entity as IHasLoggingReference;

            if (entityWithReference != null)
            {
                return(entityWithReference.Reference.ToString());
            }

            const BindingFlags flags           = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase;
            string             keyPropertyName = GetReferencePropertyForObject(entity);
            var keyProperty = entity.GetType().GetProperty(keyPropertyName, flags);

            if (keyProperty != null)
            {
                return(keyProperty.GetGetMethod().Invoke(entity, null).ToString());
            }

            throw new Exception(string.Format("Attempted to log a foreign entity that did not implement IHasLoggingReference and that did not have a property with name '{0}'. It had type {1}, and it was '{2}'",
                                              KeyPropertyName, entity.GetType(), entity));
        }
        public virtual string GetReferenceForObject(object entity)
        {
            if (entity == null)
            {
                return(null);
            }

            IHasLoggingReference entityWithReference = entity as IHasLoggingReference;

            if (entityWithReference != null)
            {
                return(entityWithReference.Reference.ToString());
            }

            const BindingFlags flags           = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase;
            string             keyPropertyName = GetReferencePropertyForObject(entity);
            var keyProperty = entity.GetType().GetProperty(keyPropertyName, flags);

            if (keyProperty != null)
            {
                return(keyProperty.GetGetMethod().Invoke(entity, null).ToString());
            }

            ObjectStateEntry entry = null;

            if (context.ObjectStateManager.TryGetObjectStateEntry(entity, out entry))
            {
                var keyMember = entry.EntityKey.EntityKeyValues.FirstOrDefault();
                if (keyMember != null && keyMember.Value != null)
                {
                    return(keyMember.Value.ToString());
                }
            }

            throw new InvalidOperationException(string.Format("Attempted to log a foreign entity that did not implement IHasLoggingReference and that did not have a property with name '{0}'. It had type {1}, and it was '{2}'",
                                                              KeyPropertyName, entity.GetType(), entity));
        }