Esempio n. 1
0
        public static InternalEntityEntry GetPrincipalEntityEntryCurrentValue(this ChangeTracker changeTracker, InternalEntityEntry targetEntity, IForeignKey foreignKey)
        {
            IEnumerable <InternalEntityEntry> targetEntities = changeTracker.GetStateManager().Entries.Where(p => Equals(p.EntityType.ClrType, foreignKey.PrincipalEntityType.ClrType));
            // IEnumerable<InternalEntityEntry> targetEntities = changeTracker.Entries().Where(p => p.  Equals(p.EntityType.ClrType, foreignKey.PrincipalEntityType.ClrType));
            InternalEntityEntry principalEntityEntry = null;

            foreach (InternalEntityEntry entityEntry in targetEntities)
            {
                bool result = true;
                for (int i = 0; i < foreignKey.Properties.Count; i++)
                {
                    object foreignValue   = targetEntity.GetCurrentValue(foreignKey.Properties[i]);
                    object principalValue = entityEntry.GetCurrentValue(foreignKey.PrincipalKey.Properties[i]);

                    if (!Equals(foreignValue, principalValue))
                    {
                        result = false;
                        break;
                    }
                }
                if (result)
                {
                    principalEntityEntry = entityEntry;
                }
            }
            return(principalEntityEntry);
        }
Esempio n. 2
0
        public void StateChanging(InternalEntityEntry entry, EntityState newState)
        {
            var entity = entry.Entity as ITranslatable;

            if (entity != null)
            {
                if (entry.EntityState == EntityState.Detached && newState == EntityState.Unchanged)
                {
                    var props = entry.EntityType.GetProperties().Where(p => p.Name.StartsWith("Name_")).ToList();

                    foreach (var prop in props)
                    {
                        entity.Translation.Set(prop.Name.Split('_')[0], entry.GetCurrentValue(prop), prop.Name.Split('_')[1]);
                    }
                }

                if (entry.EntityState == EntityState.Detached && newState == EntityState.Added)
                {
                    foreach (var item in entity.Translation.GetItems())
                    {
                        var prop = entry.EntityType.FindProperty($"{item.Property}_{item.Language}");
                        entry.SetCurrentValue(prop, item.Value);
                    }
                }
            }
        }
Esempio n. 3
0
        public T GetValue <T>(InternalEntityEntry entry, IProperty property, int index)
        {
            if (index == -1)
            {
                throw new InvalidOperationException(
                          CoreStrings.OriginalValueNotTracked(property.Name, property.DeclaringEntityType.DisplayName()));
            }

            return(IsEmpty ? entry.GetCurrentValue <T>(property) : _values.GetValue <T>(index));
        }
        public void StateChanging(InternalEntityEntry entry, EntityState newState)
        {
            Console.WriteLine(
                "  Changing {0} from {1} to {2}.",
                entry.Entity.GetType().Name, entry.EntityState, newState);

            if (newState != EntityState.Added && newState != EntityState.Modified)
            {
                return;
            }

            var set        = services.GetRequiredService <ICultureSet>();
            var type       = entry.Entity.GetType();
            var properties = type.GetProperties().Where(y => y.GetCustomAttribute <LocalizedAttribute>() != null).ToList();

            foreach (var y in properties)
            {
                try
                {
                    var origin          = entry.GetOriginalValue(entry.EntityType.FindProperty(y.Name));
                    var current         = entry.GetCurrentValue(entry.EntityType.FindProperty(y.Name));
                    var cultureProvider = services.GetRequiredService <ICultureProvider>();
                    var culture         = cultureProvider.DetermineCulture();
                    culture = set.SimplifyCulture(culture);
                    Dictionary <string, string> json;
                    if (newState == EntityState.Added)
                    {
                        json = new Dictionary <string, string>();
                    }
                    else
                    {
                        json = JsonConvert.DeserializeObject <Dictionary <string, string> >(origin.ToString());
                    }
                    if (json.ContainsKey(culture))
                    {
                        json[culture] = current.ToString();
                    }
                    else
                    {
                        json.Add(culture, current.ToString());
                    }
                    entry.SetProperty(entry.EntityType.FindProperty(y.Name), JsonConvert.SerializeObject(json));
                }
                catch
                {
                    break;
                }
            }
        }
        public void StateChanging(InternalEntityEntry entry, EntityState newState)
        {
            if (!(newState == EntityState.Added && entry.EntityState == EntityState.Detached || newState == EntityState.Modified && entry.EntityState == EntityState.Unchanged))
            {
                return;
            }

            var set        = services.GetRequiredService <ICultureSet>();
            var type       = entry.Entity.GetType();
            var properties = type.GetProperties().Where(y => y.GetCustomAttribute <LocalizedAttribute>() != null).ToList();

            foreach (var y in properties)
            {
                try
                {
                    var origin          = entry.GetOriginalValue(entry.EntityType.FindProperty(y.Name));
                    var current         = entry.GetCurrentValue(entry.EntityType.FindProperty(y.Name));
                    var cultureProvider = services.GetRequiredService <ICultureProvider>();
                    var culture         = cultureProvider.DetermineCulture();
                    culture = set.SimplifyCulture(culture);
                    Dictionary <string, string> dic;
                    if (newState == EntityState.Added)
                    {
                        dic = new Dictionary <string, string>();
                    }
                    else
                    {
                        dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(origin.ToString());
                    }
                    if (dic.ContainsKey(culture))
                    {
                        dic[culture] = current.ToString();
                    }
                    else
                    {
                        dic.Add(culture, current.ToString());
                    }
                    var json = JsonConvert.SerializeObject(dic);
                    writePropertyValue.Invoke(entry, new object[] { entry.EntityType.FindProperty(y.Name), json });
                }
                catch (Exception ex)
                {
                    break;
                }
            }
        }
            public override void AddEntity(Object dataContext, ODataResourceBase entry)
            {
                var context = (DbContext)dataContext;
                EntityEntry <TEntity> entityEntry         = context.Add(CreateEntity(context, entry));
                InternalEntityEntry   internalEntityEntry = entityEntry.GetInfrastructure();

                IReadOnlyList <IProperty> keyProperties = _entityType.FindPrimaryKey().Properties;

                for (int i = 0; i < keyProperties.Count; i++)
                {
                    if (keyProperties[i].ValueGenerated == ValueGenerated.OnAdd)
                    {
                        Object value = internalEntityEntry.GetCurrentValue(keyProperties[i]);
                        internalEntityEntry.SetTemporaryValue(keyProperties[i], value, false);
                    }
                }
            }
        public void StateChanging(InternalEntityEntry entry, EntityState newState)
        {
            Console.WriteLine(
                "  Changing {0} from {1} to {2}.",
                entry.Entity.GetType().Name, entry.EntityState, newState);

            if (newState != EntityState.Added && newState != EntityState.Modified)
                return;

            var set = services.GetRequiredService<ICultureSet>();
            var type = entry.Entity.GetType();
            var properties = type.GetProperties().Where(y => y.GetCustomAttribute<LocalizedAttribute>() != null).ToList();

            foreach (var y in properties)
            {
                try
                {
                    var origin = entry.GetOriginalValue(entry.EntityType.FindProperty(y.Name));
                    var current = entry.GetCurrentValue(entry.EntityType.FindProperty(y.Name));
                    var cultureProvider = services.GetRequiredService<ICultureProvider>();
                    var culture = cultureProvider.DetermineCulture();
                    culture = set.SimplifyCulture(culture);
                    Dictionary<string, string> json;
                    if (newState == EntityState.Added)
                        json = new Dictionary<string, string>();
                    else
                        json = JsonConvert.DeserializeObject<Dictionary<string, string>>(origin.ToString());
                    if (json.ContainsKey(culture))
                        json[culture] = current.ToString();
                    else
                        json.Add(culture, current.ToString());
                    entry.SetProperty(entry.EntityType.FindProperty(y.Name), JsonConvert.SerializeObject(json));
                }
                catch
                {
                    break;
                }
            }
        }
            public bool TryPropagate(IProperty property, InternalEntityEntry entry)
            {
                if (_write &&
                    (entry.EntityState == EntityState.Unchanged ||
                     (entry.EntityState == EntityState.Modified && !entry.IsModified(property)) ||
                     (entry.EntityState == EntityState.Added && Equals(_originalValue, entry.GetCurrentValue(property)))))
                {
                    entry[property] = _currentValue;

                    return(false);
                }

                return(_write);
            }
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public virtual IProperty FindNullPropertyInCurrentValues(InternalEntityEntry entry)
 => Properties.FirstOrDefault(p => entry.GetCurrentValue(p) == null);
Esempio n. 10
0
 public T GetValue <T>(InternalEntityEntry entry, IPropertyBase propertyBase, int index)
 => IsEmpty
         ? entry.GetCurrentValue <T>(propertyBase)
         : _values.GetValue <T>(index);
Esempio n. 11
0
            public bool TryPropagate(IProperty property, InternalEntityEntry entry)
            {
                if (_write
#pragma warning disable EF1001 // Internal EF Core API usage.
                    && (entry.EntityState == EntityState.Unchanged ||
                        (entry.EntityState == EntityState.Modified && !entry.IsModified(property)) ||
                        (entry.EntityState == EntityState.Added && Equals(_originalValue, entry.GetCurrentValue(property)))))
#pragma warning restore EF1001 // Internal EF Core API usage.
                {
                    entry[property] = _currentValue;

                    return(false);
                }

                return(_write);
            }