Esempio n. 1
0
    public EntityError Resolve(EntityManager em) {
      IsServerError = true;
      try {
        EntityType entityType = null;
        if (EntityTypeName != null) {
          var stName = StructuralType.ClrTypeNameToStructuralTypeName(EntityTypeName);
          entityType = MetadataStore.Instance.GetEntityType(stName);
          var ek = new EntityKey(entityType, KeyValues);
          Entity = em.FindEntityByKey(ek);
        }

        if (PropertyName != null) {
          PropertyName = MetadataStore.Instance.NamingConvention.ServerPropertyNameToClient(PropertyName);
        }
        if (Entity != null) {
          Property = entityType.GetProperty(PropertyName);
          var vc = new ValidationContext(this.Entity);
          vc.Property = this.Property;
          var veKey = (ErrorName ?? ErrorMessage) + (PropertyName ?? "");
          var ve = new ValidationError(null, vc, ErrorMessage, veKey);
          ve.IsServerError = true;
          this.Entity.EntityAspect.ValidationErrors.Add(ve);
        }
      } catch (Exception e) {
        ErrorMessage = ( ErrorMessage ?? "") + ":  Unable to Resolve this error: " + e.Message;
      }
      return this;
    }
Esempio n. 2
0
    protected virtual Object CreateAndPopulate(JsonContext jsonContext) {
      var jObject = jsonContext.JObject;

      JToken refToken = null;
      if (jObject.TryGetValue("$ref", out refToken)) {
        return _refMap[refToken.Value<String>()];
      }

      var objectType = jsonContext.ObjectType;
      var entityType =  _metadataStore.GetEntityType(objectType);

      // an entity type
      jsonContext.StructuralType = entityType;
      var keyValues = entityType.KeyProperties
        .Select(p => jObject[p.Name].ToObject(p.ClrType))
        .ToArray();
      var entityKey = new EntityKey(entityType, keyValues);
      var entity = _entityManager.FindEntityByKey(entityKey);
      if (entity == null) {
        entity = (IEntity)Activator.CreateInstance(objectType);
      }
      // must be called before populate
      UpdateRefMap(jObject, entity);
      return PopulateEntity(jsonContext, entity);

    }
 public void RemoveChildren(EntityKey parentEntityKey, NavigationProperty navProp) {
   var navChildrenList = GetNavChildrenList(parentEntityKey, false);
   if (navChildrenList == null) return;
   var ix = navChildrenList.IndexOf(nc => nc.NavigationProperty == navProp);
   if (ix != -1) return;
   navChildrenList.RemoveAt(ix);
   if (navChildrenList.Count == 0) {
     _map.Remove(parentEntityKey);
   }
 }
    public List<NavChildren> GetNavChildrenList(EntityKey entityKey, bool createIfNotFound) {
      List<NavChildren> navChildrenList = null;

      if (_map.TryGetValue(entityKey, out navChildrenList)) {
        return navChildrenList;
      } else {
        if (createIfNotFound) {
          navChildrenList = new List<NavChildren>();
          _map.Add(entityKey, navChildrenList);
        }
      }
      return navChildrenList;
    }
    public HashSet<IEntity> GetNavChildren(EntityKey entityKey, NavigationProperty navProp, bool createIfNotFound) {
      List<NavChildren> navChildrenList = GetNavChildrenList(entityKey, createIfNotFound);
      if (navChildrenList == null) return null;
      
      var navChildren = navChildrenList.FirstOrDefault(uc => uc.NavigationProperty == navProp);
      if (navChildren == null && createIfNotFound) {
        navChildren = new NavChildren() {NavigationProperty = navProp, Children = new HashSet<IEntity>() };
        navChildrenList.Add(navChildren);
      }

      var children = navChildren.Children;
      children.RemoveWhere( entity => entity.EntityAspect.EntityState.IsDetached());

      return children;
    }
 private Tuple<EntityKey, EntityKey> ToEntityKeys(KeyMapping keyMapping) {
   var entityTypeName = NormalizeEntityTypeName(keyMapping.EntityTypeName);
   var et = MetadataStore.Instance.GetEntityType(entityTypeName);
   var oldKey = new EntityKey(et, keyMapping.TempValue);
   var newKey = new EntityKey(et, keyMapping.RealValue);
   return Tuple.Create(oldKey, newKey);
 }
Esempio n. 7
0
 internal void ReplaceKey(EntityAspect entityAspect, EntityKey oldKey, EntityKey newKey)
 {
     _entityKeyMap.Remove(oldKey); // it may not exist if this object was just Imported or Queried.
     _entityKeyMap.Add(newKey, entityAspect);
 }
Esempio n. 8
0
        public T FindEntityByKey <T>(params Object[] values) where T : IEntity
        {
            var ek = new EntityKey(typeof(T), values);

            return((T)FindEntityByKey(ek));
        }
Esempio n. 9
0
 public T FindEntityByKey <T>(EntityKey entityKey)
 {
     return((T)FindEntityByKey(entityKey));
 }
 public void AddChild(EntityKey parentEntityKey, NavigationProperty navProp, IEntity child) {
   var navChildren = GetNavChildren(parentEntityKey, navProp, true);
   navChildren.Add(child);
 
 }