本文整理汇总了C#中INavigation.FindInverse方法的典型用法代码示例。如果您正苦于以下问题:C# INavigation.FindInverse方法的具体用法?C# INavigation.FindInverse怎么用?C# INavigation.FindInverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类INavigation
的用法示例。
在下文中一共展示了INavigation.FindInverse方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddNavigationPropertyBindings
private static void AddNavigationPropertyBindings(
IModel efModel, INavigation navi,
EdmEntityContainer container, IDictionary<IAnnotatable, IEdmElement> elementMap)
{
if (!navi.PointsToPrincipal())
{
return;
}
var naviPair = new INavigation[] { navi, navi.FindInverse() };
for (var i = 0; i < 2; i++)
{
if (naviPair[i] == null) continue;
var efEntityType = naviPair[i].DeclaringEntityType;
if (!elementMap.ContainsKey(efEntityType))
{
continue;
}
var entityType = elementMap[efEntityType] as IEdmEntityType;
var navProperty = entityType.FindProperty(
naviPair[i].Name) as IEdmNavigationProperty;
if (navProperty == null)
{
continue;
}
var entitySet = (EdmEntitySet)container.EntitySets().
First(e => e.EntityType() == entityType);
var targetEntitySet = container.EntitySets().
First(e => e.EntityType() == navProperty.ToEntityType());
entitySet.AddNavigationTarget(navProperty, targetEntitySet);
}
}
示例2: AddNavigationProperties
private static void AddNavigationProperties(
IModel efModel, INavigation navi,
EdmModel model, IDictionary<IAnnotatable, IEdmElement> elementMap)
{
if (!navi.PointsToPrincipal())
{
return;
}
var naviPair = new INavigation[] { navi, navi.FindInverse() };
var navPropertyInfos = new EdmNavigationPropertyInfo[2];
for (var i = 0; i < 2; i++)
{
var efEnd = naviPair[i];
if (efEnd == null) continue;
var efEntityType = efEnd.DeclaringEntityType;
if (!elementMap.ContainsKey(efEntityType))
{
continue;
}
var entityType = elementMap[efEntityType] as IEdmEntityType;
var efTargetEntityType = naviPair[i].GetTargetType();
if (!elementMap.ContainsKey(efTargetEntityType))
{
continue;
}
var targetEntityType = elementMap[
efTargetEntityType] as IEdmEntityType;
navPropertyInfos[i] = new EdmNavigationPropertyInfo()
{
ContainsTarget = false,
Name = naviPair[i].Name,
// TODO GitHubIssue#57: Complete EF7 to EDM model mapping
//OnDelete = efEnd.DeleteBehavior == OperationAction.Cascade
// ? EdmOnDeleteAction.Cascade : EdmOnDeleteAction.None,
OnDelete = EdmOnDeleteAction.None,
Target = targetEntityType,
TargetMultiplicity = ModelProducer.GetEdmMultiplicity(
naviPair[i]),
};
var foreignKey = naviPair[i].ForeignKey;
if (foreignKey != null && naviPair[i].PointsToPrincipal())
{
navPropertyInfos[i].DependentProperties = foreignKey.Properties
.Select(p => entityType.FindProperty(p.Name) as IEdmStructuralProperty);
navPropertyInfos[i].PrincipalProperties = foreignKey.PrincipalKey.Properties
.Select(p => targetEntityType.FindProperty(p.Name) as IEdmStructuralProperty);
}
}
if (navPropertyInfos[0] == null && navPropertyInfos[1] != null)
{
var efEntityType = navi.GetTargetType();
var entityType = elementMap[efEntityType] as EdmEntityType;
if (entityType.FindProperty(navPropertyInfos[1].Name) == null)
{
entityType.AddUnidirectionalNavigation(navPropertyInfos[1]);
}
}
if (navPropertyInfos[0] != null && navPropertyInfos[1] == null)
{
var efEntityType = navi.DeclaringEntityType;
var entityType = elementMap[efEntityType] as EdmEntityType;
if (entityType.FindProperty(navPropertyInfos[0].Name) == null)
{
entityType.AddUnidirectionalNavigation(navPropertyInfos[0]);
}
}
if (navPropertyInfos[0] != null && navPropertyInfos[1] != null)
{
var efEntityType = navi.DeclaringEntityType;
var entityType = elementMap[efEntityType] as EdmEntityType;
if (entityType.FindProperty(navPropertyInfos[0].Name) == null)
{
entityType.AddBidirectionalNavigation(
navPropertyInfos[0], navPropertyInfos[1]);
}
}
}
示例3: ConditionallyClearInverse
private void ConditionallyClearInverse(InternalEntityEntry entry, INavigation navigation, object entity)
{
var inverse = navigation.FindInverse();
if (inverse != null)
{
if (inverse.IsCollection())
{
_collectionAccessorSource.GetAccessor(inverse).Remove(entity, entry.Entity);
}
else
{
if (ReferenceEquals(_getterSource.GetAccessor(inverse).GetClrValue(entity), entry.Entity))
{
_setterSource.GetAccessor(inverse).SetClrValue(entity, null);
}
}
entry.StateManager.GetOrCreateEntry(entity).RelationshipsSnapshot.TakeSnapshot(inverse);
}
}
示例4: SetInverse
private void SetInverse(InternalEntityEntry entry, INavigation navigation, object entity)
{
var inverse = navigation.FindInverse();
if (inverse != null)
{
var inverseEntry = entry.StateManager.GetOrCreateEntry(entity);
if (inverse.IsCollection())
{
var collectionAccessor = _collectionAccessorSource.GetAccessor(inverse);
if (!collectionAccessor.Contains(entity, entry.Entity))
{
collectionAccessor.Add(entity, entry.Entity);
}
}
else
{
var oldEntity = _getterSource.GetAccessor(inverse).GetClrValue(entity);
if (oldEntity != null
&& oldEntity != entry.Entity)
{
var oldEntry = entry.StateManager.GetOrCreateEntry(oldEntity);
if (navigation.PointsToPrincipal())
{
Unfixup(navigation, inverseEntry, oldEntry);
SetNullForeignKey(oldEntry, navigation.ForeignKey.Properties);
}
else
{
Unfixup(navigation, oldEntry, inverseEntry);
}
}
_setterSource.GetAccessor(inverse).SetClrValue(entity, entry.Entity);
}
inverseEntry.RelationshipsSnapshot.TakeSnapshot(inverse);
}
}
示例5: NavigationReferenceChanged
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual void NavigationReferenceChanged(InternalEntityEntry entry, INavigation navigation, object oldValue, object newValue)
{
if (_inFixup)
{
return;
}
var foreignKey = navigation.ForeignKey;
var stateManager = entry.StateManager;
var inverse = navigation.FindInverse();
var oldTargetEntry = oldValue == null ? null : stateManager.TryGetEntry(oldValue);
if (oldTargetEntry?.EntityState == EntityState.Detached)
{
oldTargetEntry = null;
}
var newTargetEntry = newValue == null ? null : stateManager.TryGetEntry(newValue);
if (newTargetEntry?.EntityState == EntityState.Detached)
{
newTargetEntry = null;
}
try
{
_inFixup = true;
if (navigation.IsDependentToPrincipal())
{
if (newValue != null)
{
if (newTargetEntry != null)
{
if (foreignKey.IsUnique)
{
// Navigation points to principal. Find the dependent that previously pointed to that principal and
// null out its FKs and navigation property. A.k.a. reference stealing.
// However, if the FK has already been changed or the reference is already set to point
// to something else, then don't change it.
var victimDependentEntry = stateManager.GetDependents(newTargetEntry, foreignKey).FirstOrDefault();
if (victimDependentEntry != null
&& victimDependentEntry != entry)
{
ConditionallyNullForeignKeyProperties(victimDependentEntry, newTargetEntry, foreignKey);
if (ReferenceEquals(victimDependentEntry[navigation], newTargetEntry.Entity))
{
SetNavigation(victimDependentEntry, navigation, null);
}
}
}
// Set the FK properties to reflect the change to the navigation.
SetForeignKeyProperties(entry, newTargetEntry, foreignKey, setModified: true);
}
}
else
{
// Null the FK properties to reflect that the navigation has been nulled out.
ConditionallyNullForeignKeyProperties(entry, oldTargetEntry, foreignKey);
}
if (inverse != null)
{
var collectionAccessor = inverse.IsCollection() ? inverse.GetCollectionAccessor() : null;
// Set the inverse reference or add the entity to the inverse collection
if (newTargetEntry != null)
{
SetReferenceOrAddToCollection(newTargetEntry, inverse, collectionAccessor, entry.Entity);
}
// Remove the entity from the old collection, or null the old inverse unless it was already
// changed to point to something else
if (oldTargetEntry != null)
{
if (collectionAccessor != null)
{
RemoveFromCollection(oldTargetEntry, inverse, collectionAccessor, entry.Entity);
}
else if (ReferenceEquals(oldTargetEntry[inverse], entry.Entity))
{
SetNavigation(oldTargetEntry, inverse, null);
}
}
}
}
else
{
Debug.Assert(foreignKey.IsUnique);
if (newTargetEntry != null)
{
// Navigation points to dependent and is 1:1. Find the principal that previously pointed to that
// dependent and null out its navigation property. A.k.a. reference stealing.
// However, if the reference is already set to point to something else, then don't change it.
//.........这里部分代码省略.........
示例6: NavigationCollectionChanged
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual void NavigationCollectionChanged(
InternalEntityEntry entry,
INavigation navigation,
IEnumerable<object> added,
IEnumerable<object> removed)
{
if (_inFixup)
{
return;
}
var foreignKey = navigation.ForeignKey;
var stateManager = entry.StateManager;
var inverse = navigation.FindInverse();
var collectionAccessor = navigation.GetCollectionAccessor();
foreach (var oldValue in removed)
{
var oldTargetEntry = stateManager.TryGetEntry(oldValue);
if (oldTargetEntry != null
&& oldTargetEntry.EntityState != EntityState.Detached)
{
try
{
_inFixup = true;
// Null FKs and navigations of dependents that have been removed, unless they
// have already been changed.
ConditionallyNullForeignKeyProperties(oldTargetEntry, entry, foreignKey);
if (inverse != null
&& ReferenceEquals(oldTargetEntry[inverse], entry.Entity))
{
SetNavigation(oldTargetEntry, inverse, null);
}
entry.RemoveFromCollectionSnapshot(navigation, oldValue);
}
finally
{
_inFixup = false;
}
}
}
foreach (var newValue in added)
{
var newTargetEntry = stateManager.GetOrCreateEntry(newValue);
if (newTargetEntry.EntityState != EntityState.Detached)
{
try
{
_inFixup = true;
// For a dependent added to the collection, remove it from the collection of
// the principal entity that it was previously part of
var oldPrincipalEntry = stateManager.GetPrincipal(newTargetEntry, foreignKey);
if (oldPrincipalEntry != null)
{
RemoveFromCollection(oldPrincipalEntry, navigation, collectionAccessor, newValue);
}
// Set the FK properties on added dependents to match this principal
SetForeignKeyProperties(newTargetEntry, entry, foreignKey, setModified: true);
// Set the inverse navigation to point to this principal
SetNavigation(newTargetEntry, inverse, entry.Entity);
}
finally
{
_inFixup = false;
}
}
else
{
stateManager.RecordReferencedUntrackedEntity(newValue, navigation, entry);
_attacher.AttachGraph(newTargetEntry, EntityState.Added);
}
entry.AddToCollectionSnapshot(navigation, newValue);
}
}