示例#1
0
        private bool IsEntityRequiredForeignKeyEmpty(DbEntityEntry entityEntry, string errProp)
        {
            RelationshipManager relMgr =
                ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager
                .GetRelationshipManager(entityEntry.Entity);
            IEnumerable <IRelatedEnd> relEnds = relMgr.GetAllRelatedEnds();
            IRelatedEnd relEnd = relEnds.Where(r =>
            {
                var elementType      = (EntityTypeBase)r.RelationshipSet.ElementType;
                var metadataProperty = elementType.MetadataProperties.GetValue(
                    "ReferentialConstraints", false);
                var referentialConstraints =
                    (
                        System.Data.Entity.Core.Metadata.Edm.ReadOnlyMetadataCollection
                        <ReferentialConstraint>)metadataProperty.Value;
                if (referentialConstraints.Any(constraint =>
                {
                    return
                    (constraint.ToProperties.Any(
                         t => t.Nullable == false && t.IsPrimitiveType && t.Name == errProp));
                }))
                {
                    return(true);
                }
                return(false);
            }).FirstOrDefault();
            bool isEntityRequiredForeignKeyEmpty = relEnd != null;

            return(isEntityRequiredForeignKeyEmpty);
        }
示例#2
0
        protected T GetObjectByKey(T entity, bool lazyLoad)
        {
            EntityKey key = _context.CreateEntityKey(_entitySetName, entity);
            object    result;

            if (_context.TryGetObjectByKey(key, out result))
            {
                if (!lazyLoad)
                {
                    RelationshipManager relation = ((IEntityWithRelationships)result).RelationshipManager;
                    foreach (var relatedEnd in relation.GetAllRelatedEnds())
                    {
                        var relatedRef = relatedEnd as EntityReference;
                        if (relatedRef != null && !relatedRef.IsLoaded)
                        {
                            relatedRef.Load();
                        }
                    }
                }
                return(result as T);
            }
            return(null);
        }