当前位置: 首页>>代码示例>>C#>>正文


C# DbEntityEntry.Reference方法代码示例

本文整理汇总了C#中DbEntityEntry.Reference方法的典型用法代码示例。如果您正苦于以下问题:C# DbEntityEntry.Reference方法的具体用法?C# DbEntityEntry.Reference怎么用?C# DbEntityEntry.Reference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DbEntityEntry的用法示例。


在下文中一共展示了DbEntityEntry.Reference方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateEntity

        public static void ValidateEntity(DbContext context, DbEntityEntry entity, Type type)
        {
            if (entity.State == System.Data.EntityState.Modified)
            {
                if (!_parentAttributes.ContainsKey(type))
                {
                    var properties = from attributedProperty in type.GetProperties()
                                     select new
                                     {
                                         attributedProperty,
                                         attributes = attributedProperty.GetCustomAttributes(true)
                                             .Where(attribute => attribute is ParentAttribute)
                                     };
                    properties = properties.Where(p => p.attributes.Any());
                    _parentAttributes.Add(type,
                                          properties.Any()
                                              ? properties.First().attributedProperty.Name
                                              : string.Empty);
                }

                if (!string.IsNullOrEmpty(_parentAttributes[type]))
                {
                    if (entity.Reference(_parentAttributes[type]).CurrentValue == null)
                    {
                        context.Set(type).Remove(entity.Entity);
                    }
                }
            }
        }
开发者ID:WimAtIHomer,项目名称:GenericRepository,代码行数:29,代码来源:ParentValidator.cs

示例2: ValidateEntity

        /// <summary>
        /// Validates the entity.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="type">The type.</param>
        /// <returns>true if entity is removed</returns>
        public static bool ValidateEntity(DbContext context, DbEntityEntry entity, Type type)
        {
            if (entity.State == EntityState.Modified)
            {
                if(IsRemovable(type))
                {
					foreach (var parentAttribute in _parentAttributes[type])
	                {
						//Parent must have ForeignKey
		                if (String.IsNullOrWhiteSpace(parentAttribute.ForeignKey))
		                {
			                continue;
		                }

						//Navigation property must be null
		                if (entity.Reference(parentAttribute.Name).CurrentValue != null)
		                {
			                continue;
		                }
		                var fkProperty = entity.Property(parentAttribute.ForeignKey);

						//ForegnKey must be modified
						if (!fkProperty.IsModified)
						{
							continue;
						}

		                var isFkNullable = ReferenceEquals(fkProperty.CurrentValue, null);
		                if (!isFkNullable)
		                {
			                var fkType = fkProperty.CurrentValue.GetType();
			                isFkNullable = !fkType.IsValueType || Nullable.GetUnderlyingType(fkType) != null;
		                }

		                //ForeignKey must be null if type is nullable 
						//(Thats how EF work when removing item from collection)
						if (isFkNullable && fkProperty.CurrentValue != null)
		                {
			                continue;
		                }

		                context.Set(type).Remove(entity.Entity);
		                return true;
	                }
                }
            }
            return false;
        }
开发者ID:Wdovin,项目名称:vc-community,代码行数:55,代码来源:ParentValidator.cs

示例3: LoadParent

        //loads all the parent categories for bread crumbs
        private void LoadParent(DbEntityEntry<Category> cat)
        {
            cat.Reference(x => x.ParentCategory).Load();

            if (cat.Entity.ParentId != null)
                LoadParent(RepoCategories.Entry(cat.Entity.ParentCategory));
        }
开发者ID:ctrlShiftBryan,项目名称:TKProto,代码行数:8,代码来源:ResourceService.cs


注:本文中的DbEntityEntry.Reference方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。