public void Apply_should_introduce_constraint_when_one_to_one() { var associationType = new EdmAssociationType().Initialize(); var entityType1 = new EdmEntityType(); entityType1.DeclaredKeyProperties.Add(new EdmProperty().AsPrimitive()); var entityType2 = new EdmEntityType(); entityType2.DeclaredKeyProperties.Add(new EdmProperty().AsPrimitive()); associationType.SourceEnd.EntityType = entityType2; associationType.TargetEnd.EntityType = entityType1; associationType.MarkPrincipalConfigured(); ((IEdmConvention<EdmAssociationType>)new OneToOneConstraintIntroductionConvention()) .Apply(associationType, new EdmModel()); Assert.NotNull(associationType.Constraint); Assert.Equal(1, associationType.Constraint.DependentProperties.Count); }
private void ConfigureDependentBehavior( EdmAssociationType associationType, EdmModel model, EntityTypeConfiguration entityTypeConfiguration) { //Contract.Requires(associationType != null); //Contract.Requires(model != null); //Contract.Requires(entityTypeConfiguration != null); EdmAssociationEnd principalEnd; EdmAssociationEnd 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) .Where(np => np.GetClrPropertyInfo().IsSameAs(NavigationProperty)) .Single(); 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) .Where(aset => aset.ElementType == associationType).Single(); 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)); }