/// <summary> /// Retrieves an existing entity type, or creates a new one if no matching entity type is found. /// </summary> /// <param name="entityTypeName">Entity type name.</param> /// <param name="baseType">Base type that this entity type will inherit from if creating a new type.</param> /// <returns>A ModelEntityType object.</returns> public ModelEntityType GetOrCreateEntityType(string entityTypeName, ModelEntityType baseType) { try { ModelEntityType modelEntityType = EntityTypes.FirstOrDefault(et => et.Name.Equals(entityTypeName)); if (modelEntityType == null) { modelEntityType = AddEntityType(entityTypeName, baseType); } return(modelEntityType); } catch (Exception ex) { try { if (!ex.Data.Contains("EDMXType")) { ex.Data.Add("EDMXType", this.GetType().Name); } if (!ex.Data.Contains("EDMXObjectName")) { ex.Data.Add("EDMXObjectName", this.ContainerName); } } catch { } throw; } }
/// <summary> /// Retrieves an existing entitytype based on a conceptual model entittype, or creates a new one if a match can not be found. /// </summary> /// <param name="modelEntityType">Conceptual model entity type to match.</param> /// <returns>A StoreEntityType object.</returns> public StoreEntityType GetOrCreateEntityType(ModelEntityType modelEntityType) { StoreEntityType storeEntityType = EntityTypes.FirstOrDefault(et => et.Name.Equals(modelEntityType.Name, StringComparison.InvariantCultureIgnoreCase)); if (storeEntityType == null) { storeEntityType = AddEntityType(modelEntityType); } return(storeEntityType); }
public List <EntityIndex> GetRelatedIndexes(string entityType, int entityId) { var refType = EntityTypes.FirstOrDefault(x => x.Name == entityType); if (refType == null) { return(new List <EntityIndex>()); } List <EntityIndex> result = EntityIndexes.Where(ei => ei.Keywords.Count(kw => kw.ReferenceEntityTypeID == refType.ID && kw.ReferenceEntityKey == entityId) > 0).Distinct().ToList(); return(result); }
static EF7DataContext() { var t = typeof(TEntity); EntityType = EntityTypes.FirstOrDefault(x => x.Name == (t.Name.Substring(1))); ctxType = ContextTypes.FirstOrDefault(x => x.BaseType != null && x.BaseType.Name.Contains("DbContext")); if (EntityType == null) { throw new InvalidOperationException("DataType Is not Found"); } if (ctxType == null) { throw new InvalidOperationException("DBContext Is not Found"); } }
static EntityViewDataContext() { var bt = typeof(TEntityView).GetInterfaces().FirstOrDefault(x => x.Name.Contains("IEntityView")); TEntity = bt.GetGenericArguments().First(); EntityType = EntityTypes.FirstOrDefault(x => x.Name == (TEntity.Name.Substring(1))); ViewType = EntityTypes.FirstOrDefault(x => x.Name == (typeof(TEntityView).Name.Substring(1))); ctxType = ContextTypes.FirstOrDefault(x => x.BaseType != null && x.BaseType.Name.Contains("DbContext")); if (ViewType == null) { throw new InvalidOperationException("ViewType Is not Found"); } if (ctxType == null) { throw new InvalidOperationException("DBContext Is not Found"); } }
public override void OnShown() { Entities = new ObservableCollection <EntityListerRow>(); SelectedEntityType = EntityTypes.FirstOrDefault(); base.OnShown(); }
public IEntityType TryGetEntityType(string name) { return(EntityTypes.FirstOrDefault(e => e.Name == name)); }
public IEntityType TryGetEntityType(Type type) { return(EntityTypes.FirstOrDefault(e => e.Type == type)); }
public IEntityType FindEntityType([NotNull] string name, [NotNull] string definingNavigationName, [NotNull] IEntityType definingEntityType) { return(EntityTypes.FirstOrDefault(x => x.Name == name && x.DefiningNavigationName == definingNavigationName && x.DefiningEntityType == x.DefiningEntityType)); }
public IEntityType FindEntityType([NotNull] string name) { return(EntityTypes.FirstOrDefault(x => x.Name == name)); }
public IEntityType FindEntityType(string name) => EntityTypes.FirstOrDefault(e => e.Name == name);
public IEntityType FindEntityType(Type type) => EntityTypes.FirstOrDefault(e => e.ClrType == type);
public virtual IList <EntityBag> MergeEntities(IEnumerable <EntityBag> entities, out IEnumerable <EntityBag> unmappedEntities) { if (entities == null) { throw new ArgumentNullException(nameof(entities)); } var unmappeds = new List <EntityBag>(); var entityList = entities as IList <EntityBag> ?? entities.ToList(); var mergeList = new Dictionary <ObjectStateEntry, EntityBag>(); foreach (var entityBag in entityList) { var entity = entityBag.Entity; var entityType = EntityTypes.FirstOrDefault(et => et.Name == entity.GetType().Name); EntitySetBase set = null; if (entityType != null) { set = FindEntitySet(entityType); } if (set == null) { unmappeds.Add(entityBag); continue; } var state = entityBag.EntityState; // attach entity to entity set _objectContext.AddObject(set.Name, entity); var entry = _objectContext.ObjectStateManager.GetObjectStateEntry(entity); entry.ChangeState(EFEntityState.Unchanged); mergeList.Add(entry, entityBag); // set original values for modified entities if (state != EntityState.Modified) { continue; } var originalValues = entry.GetUpdatableOriginalValues(); foreach (var originalValue in entityBag.OriginalValues) { var propertyPaths = originalValue.Key.Split('.'); var loopOriginalValues = originalValues; StructuralType loopType = entityType; EdmMember property = null; string lastPropertyName; if (propertyPaths.Length > 1) { for (var i = 0; i < propertyPaths.Length - 1; i++) { var propertyName = propertyPaths[i]; property = loopType.Members.FirstOrDefault(p => p.Name == propertyName); if (property == null) { break; } var ordinal = loopOriginalValues.GetOrdinal(propertyName); loopOriginalValues = (OriginalValueRecord)loopOriginalValues.GetValue(ordinal); loopType = (StructuralType)property.TypeUsage.EdmType; } if (property == null) { continue; } lastPropertyName = propertyPaths[propertyPaths.Length - 1]; } else { lastPropertyName = originalValue.Key; } property = loopType.Members.FirstOrDefault(p => p.Name == lastPropertyName); if (property == null) { continue; } // if modified property is a ComplexType then set all properties of ComplexType to modified. if (property.TypeUsage.EdmType is ComplexType complexType) { PopulateComplexType(loopOriginalValues, property.Name, originalValue.Value, complexType); } else { var ordinal = loopOriginalValues.GetOrdinal(property.Name); loopOriginalValues.SetValue(ordinal, originalValue.Value); } } } foreach (var merge in mergeList) { // and fix the entity state, and relations will also be fixed. if (merge.Value.EntityState == EntityState.Modified) { if (merge.Key.State != EFEntityState.Modified && merge.Value.ForceUpdate) { merge.Key.ChangeState(EFEntityState.Modified); } } else { merge.Key.ChangeState((EFEntityState)merge.Value.EntityState); } } unmappedEntities = unmappeds; return(mergeList.Select(m => m.Value).ToList()); }