本文整理汇总了C#中System.Data.Entity.Core.Metadata.Edm.AssociationType.TryGuessPrincipalAndDependentEnds方法的典型用法代码示例。如果您正苦于以下问题:C# AssociationType.TryGuessPrincipalAndDependentEnds方法的具体用法?C# AssociationType.TryGuessPrincipalAndDependentEnds怎么用?C# AssociationType.TryGuessPrincipalAndDependentEnds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.Entity.Core.Metadata.Edm.AssociationType
的用法示例。
在下文中一共展示了AssociationType.TryGuessPrincipalAndDependentEnds方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UpdatePrincipalTables
private static void UpdatePrincipalTables(
DbDatabaseMapping databaseMapping, EntityType toTable, bool removeFks,
AssociationType associationType, EntityType et)
{
AssociationEndMember principalEnd, dependentEnd;
var endsToCheck = new List<AssociationEndMember>();
if (associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
{
endsToCheck.Add(principalEnd);
}
else if (associationType.SourceEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many
&& associationType.TargetEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many)
{
// many to many consider both ends
endsToCheck.Add(associationType.SourceEnd);
endsToCheck.Add(associationType.TargetEnd);
}
else
{
// 1:1 and 0..1:0..1
endsToCheck.Add(associationType.SourceEnd);
}
foreach (var end in endsToCheck)
{
if (end.GetEntityType() == et)
{
IEnumerable<KeyValuePair<EntityType, IEnumerable<EdmProperty>>> dependentTableInfos;
if (associationType.Constraint != null)
{
var originalDependentType = associationType.GetOtherEnd(end).GetEntityType();
var allDependentTypes = databaseMapping.Model.GetSelfAndAllDerivedTypes(originalDependentType);
dependentTableInfos =
allDependentTypes.Select(t => databaseMapping.GetEntityTypeMapping(t)).Where(
dm => dm != null)
.SelectMany(
dm => dm.MappingFragments
.Where(
tmf => associationType.Constraint.ToProperties
.All(
p =>
tmf.ColumnMappings.Any(
pm => pm.PropertyPath.First() == p))))
.Distinct((f1, f2) => f1.Table == f2.Table)
.Select(
df =>
new KeyValuePair<EntityType, IEnumerable<EdmProperty>>(
df.Table,
df.ColumnMappings.Where(
pm =>
associationType.Constraint.ToProperties.Contains(
pm.PropertyPath.First())).Select(
pm => pm.ColumnProperty)));
}
else
{
// IA
var associationSetMapping =
databaseMapping.EntityContainerMappings
.Single().AssociationSetMappings
.Single(asm => asm.AssociationSet.ElementType == associationType);
var dependentTable = associationSetMapping.Table;
var propertyMappings = associationSetMapping.SourceEndMapping.AssociationEnd == end
? associationSetMapping.SourceEndMapping.PropertyMappings
: associationSetMapping.TargetEndMapping.PropertyMappings;
var dependentColumns = propertyMappings.Select(pm => pm.Column);
dependentTableInfos = new[]
{
new KeyValuePair
<EntityType, IEnumerable<EdmProperty>>(
dependentTable, dependentColumns)
};
}
foreach (var tableInfo in dependentTableInfos)
{
foreach (
var fk in
tableInfo.Key.ForeignKeyBuilders.Where(
fk => fk.DependentColumns.SequenceEqual(tableInfo.Value)).ToArray(
))
{
if (removeFks)
{
tableInfo.Key.RemoveForeignKey(fk);
}
else if (fk.GetAssociationType() == null || fk.GetAssociationType() == associationType)
{
fk.PrincipalTable = toTable;
}
}
}
}
}
}
示例2: GenerateIndependentAssociationType
private void GenerateIndependentAssociationType(
AssociationType associationType, DbDatabaseMapping databaseMapping)
{
DebugCheck.NotNull(associationType);
DebugCheck.NotNull(databaseMapping);
AssociationEndMember principalEnd, dependentEnd;
if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
{
if (!associationType.IsPrincipalConfigured())
{
throw Error.UnableToDeterminePrincipal(
associationType.SourceEnd.GetEntityType().GetClrType(),
associationType.TargetEnd.GetEntityType().GetClrType());
}
principalEnd = associationType.SourceEnd;
dependentEnd = associationType.TargetEnd;
}
var dependentEntityTypeMapping = GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType());
var dependentTable = dependentEntityTypeMapping
.MappingFragments
.First()
.Table;
var associationSetMapping
= GenerateAssociationSetMapping(
associationType, databaseMapping, principalEnd, dependentEnd, dependentTable);
GenerateIndependentForeignKeyConstraint(
databaseMapping,
principalEnd.GetEntityType(),
dependentEnd.GetEntityType(),
dependentTable,
associationSetMapping,
associationSetMapping.SourceEndMapping,
associationType.Name,
principalEnd);
foreach (var property in dependentEnd.GetEntityType().KeyProperties())
{
associationSetMapping.TargetEndMapping
.AddPropertyMapping(
new ScalarPropertyMapping(
property,
dependentEntityTypeMapping.GetPropertyMapping(property).ColumnProperty));
}
}
示例3: ConfigureDependentBehavior
private void ConfigureDependentBehavior(
AssociationType associationType, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
{
DebugCheck.NotNull(associationType);
DebugCheck.NotNull(model);
DebugCheck.NotNull(entityTypeConfiguration);
AssociationEndMember principalEnd;
AssociationEndMember dependentEnd;
if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
{
if (IsNavigationPropertyDeclaringTypePrincipal.HasValue)
{
associationType.MarkPrincipalConfigured();
var navProp = model.Namespaces
.SelectMany(ns => ns.EntityTypes)
.SelectMany(et => et.DeclaredNavigationProperties)
.Single(np => np.GetClrPropertyInfo().IsSameAs(NavigationProperty));
principalEnd = IsNavigationPropertyDeclaringTypePrincipal.Value
? associationType.GetOtherEnd(navProp.ResultEnd)
: navProp.ResultEnd;
dependentEnd = associationType.GetOtherEnd(principalEnd);
if (associationType.SourceEnd != principalEnd)
{
// need to move around source to be principal, target to be dependent so Edm services will use the correct
// principal and dependent ends. The Edm default Db + mapping service tries to guess principal/dependent
// based on multiplicities, but if it can't figure it out, it will use source as principal and target as dependent
associationType.SourceEnd = principalEnd;
associationType.TargetEnd = dependentEnd;
var associationSet
= model.Containers
.SelectMany(ct => ct.AssociationSets)
.Single(aset => aset.ElementType == associationType);
var sourceSet = associationSet.SourceSet;
associationSet.SourceSet = associationSet.TargetSet;
associationSet.TargetSet = sourceSet;
}
}
if (principalEnd == null)
{
dependentEnd = associationType.TargetEnd;
}
}
ConfigureConstraint(associationType, dependentEnd, entityTypeConfiguration);
ConfigureDeleteAction(associationType.GetOtherEnd(dependentEnd));
}
示例4: TryGuessPrincipalAndDependentEnds_should_return_correct_ends_for_optional_to_required
public void TryGuessPrincipalAndDependentEnds_should_return_correct_ends_for_optional_to_required()
{
var associationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
associationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
associationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
associationType.SourceEnd.RelationshipMultiplicity = RelationshipMultiplicity.ZeroOrOne;
associationType.TargetEnd.RelationshipMultiplicity = RelationshipMultiplicity.One;
AssociationEndMember principalEnd, dependentEnd;
associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd);
Assert.Same(associationType.TargetEnd, principalEnd);
Assert.Same(associationType.SourceEnd, dependentEnd);
}