本文整理汇总了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);
}
}
}
}
示例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;
}
示例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));
}