本文整理汇总了C#中System.Data.Entity.Core.Metadata.Edm.EdmModel.AddAssociationType方法的典型用法代码示例。如果您正苦于以下问题:C# EdmModel.AddAssociationType方法的具体用法?C# EdmModel.AddAssociationType怎么用?C# EdmModel.AddAssociationType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.Entity.Core.Metadata.Edm.EdmModel
的用法示例。
在下文中一共展示了EdmModel.AddAssociationType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HasCascadeDeletePath_should_return_true_for_transitive_cascade
public void HasCascadeDeletePath_should_return_true_for_transitive_cascade()
{
var model = new EdmModel(DataSpace.CSpace);
var entityTypeA = model.AddEntityType("A");
var entityTypeB = model.AddEntityType("B");
var entityTypeC = model.AddEntityType("B");
var associationTypeA
= new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
{
SourceEnd = new AssociationEndMember("S", entityTypeA),
TargetEnd = new AssociationEndMember("T", entityTypeB)
};
associationTypeA.SourceEnd.DeleteBehavior = OperationAction.Cascade;
model.AddAssociationType(associationTypeA);
var associationTypeB
= new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
{
SourceEnd = new AssociationEndMember("S", entityTypeB),
TargetEnd = new AssociationEndMember("T", entityTypeC)
};
associationTypeB.SourceEnd.DeleteBehavior = OperationAction.Cascade;
model.AddAssociationType(associationTypeB);
Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeB));
Assert.True(model.HasCascadeDeletePath(entityTypeB, entityTypeC));
Assert.True(model.HasCascadeDeletePath(entityTypeA, entityTypeC));
Assert.False(model.HasCascadeDeletePath(entityTypeB, entityTypeA));
Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeB));
Assert.False(model.HasCascadeDeletePath(entityTypeC, entityTypeA));
}
示例2: Apply_should_not_match_key_that_is_also_an_fk
public void Apply_should_not_match_key_that_is_also_an_fk()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = new EntityType("E", "N", DataSpace.CSpace);
var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));
entityType.AddKeyMember(property);
var associationType
= model.AddAssociationType(
"A", new EntityType("E", "N", DataSpace.CSpace), RelationshipMultiplicity.ZeroOrOne,
entityType, RelationshipMultiplicity.Many);
associationType.Constraint
= new ReferentialConstraint(
associationType.SourceEnd,
associationType.TargetEnd,
new[] { property },
new[] { property });
(new StoreGeneratedIdentityKeyConvention())
.Apply(entityType, new DbModel(model, null));
Assert.Null(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
}
示例3: Configure_should_configure_inverse
public void Configure_should_configure_inverse()
{
var inverseMockPropertyInfo = new MockPropertyInfo();
var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo())
{
InverseNavigationProperty = inverseMockPropertyInfo
};
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));
var inverseAssociationType = new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace);
inverseAssociationType.SourceEnd = new AssociationEndMember("S", new EntityType("E", "N", DataSpace.CSpace));
inverseAssociationType.TargetEnd = new AssociationEndMember("T", new EntityType("E", "N", DataSpace.CSpace));
var model = new EdmModel(DataSpace.CSpace);
model.AddAssociationType(inverseAssociationType);
var inverseNavigationProperty
= model.AddEntityType("T")
.AddNavigationProperty("N", inverseAssociationType);
inverseNavigationProperty.SetClrPropertyInfo(inverseMockPropertyInfo);
navigationPropertyConfiguration.Configure(
new NavigationProperty("N", TypeUsage.Create(associationType.TargetEnd.GetEntityType()))
{
RelationshipType = associationType
}, model, new EntityTypeConfiguration(typeof(object)));
Assert.Same(associationType, inverseNavigationProperty.Association);
Assert.Same(associationType.SourceEnd, inverseNavigationProperty.ResultEnd);
Assert.Same(associationType.TargetEnd, inverseNavigationProperty.FromEndMember);
Assert.Equal(0, model.AssociationTypes.Count());
}
示例4: HasCascadeDeletePath_should_return_true_for_self_ref_cascade
public void HasCascadeDeletePath_should_return_true_for_self_ref_cascade()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = model.AddEntityType("A");
var associationType
= new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)
{
SourceEnd = new AssociationEndMember("S", entityType)
};
associationType.TargetEnd = new AssociationEndMember("T", associationType.SourceEnd.GetEntityType());
associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;
model.AddAssociationType(associationType);
Assert.True(model.HasCascadeDeletePath(entityType, entityType));
}
示例5: Apply_generates_constraint_when_simple_fk
public void Apply_generates_constraint_when_simple_fk()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = model.AddEntityType("E");
var associationType = model.AddAssociationType(
"A",
entityType, RelationshipMultiplicity.ZeroOrOne,
entityType, RelationshipMultiplicity.Many);
var navigationProperty = entityType.AddNavigationProperty("N", associationType);
var property = EdmProperty.Primitive("Fk", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
entityType.AddMember(property);
var fkProperty = property;
var foreignKeyAnnotation = new ForeignKeyAttribute("Fk");
navigationProperty.Annotations.SetClrAttributes(new[] { foreignKeyAnnotation });
((IEdmConvention<NavigationProperty>)new ForeignKeyNavigationPropertyAttributeConvention())
.Apply(navigationProperty, model);
Assert.NotNull(associationType.Constraint);
Assert.True(associationType.Constraint.ToProperties.Contains(fkProperty));
}
示例6: CreateModelFixture
private static EdmModel CreateModelFixture(
out EntityType declaringEntityType, out EntityType complexEntityType)
{
var model = new EdmModel(DataSpace.CSpace);
declaringEntityType = model.AddEntityType("E");
complexEntityType = model.AddEntityType("C");
var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
complexEntityType.AddMember(property);
var associationType
= model.AddAssociationType(
"A",
declaringEntityType, RelationshipMultiplicity.Many,
complexEntityType, RelationshipMultiplicity.ZeroOrOne);
declaringEntityType.AddNavigationProperty("E.C", associationType);
return model;
}
示例7: Generate_can_map_independent_association_type
public void Generate_can_map_independent_association_type()
{
var model = new EdmModel(DataSpace.CSpace);
var principalEntityType = model.AddEntityType("P");
var type = typeof(object);
principalEntityType.GetMetadataProperties().SetClrType(type);
var property = EdmProperty.CreatePrimitive("Id1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
principalEntityType.AddMember(property);
var idProperty1 = property;
principalEntityType.AddKeyMember(idProperty1);
var property1 = EdmProperty.CreatePrimitive("Id2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
principalEntityType.AddMember(property1);
var idProperty2 = property1;
principalEntityType.AddKeyMember(idProperty2);
var dependentEntityType = model.AddEntityType("D");
var type1 = typeof(string);
dependentEntityType.GetMetadataProperties().SetClrType(type1);
model.AddEntitySet("PSet", principalEntityType);
model.AddEntitySet("DSet", dependentEntityType);
var associationType
= model.AddAssociationType(
"P_D",
principalEntityType, RelationshipMultiplicity.One,
dependentEntityType, RelationshipMultiplicity.Many);
model.AddAssociationSet("P_DSet", associationType);
associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;
var databaseMapping = CreateDatabaseMappingGenerator().Generate(model);
var foreignKeyConstraint
=
databaseMapping.GetEntityTypeMapping(dependentEntityType).MappingFragments.Single().Table.ForeignKeyBuilders.Single();
Assert.Equal(2, foreignKeyConstraint.DependentColumns.Count());
Assert.Equal(associationType.Name, foreignKeyConstraint.Name);
Assert.Equal(1, databaseMapping.EntityContainerMappings.Single().AssociationSetMappings.Count());
Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);
var foreignKeyColumn = foreignKeyConstraint.DependentColumns.First();
Assert.False(foreignKeyColumn.Nullable);
Assert.Equal("P_Id1", foreignKeyColumn.Name);
}
示例8: Apply_generates_constraint_when_composite_fk
public void Apply_generates_constraint_when_composite_fk()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = model.AddEntityType("E");
var associationType = model.AddAssociationType(
"A",
entityType, RelationshipMultiplicity.ZeroOrOne,
entityType, RelationshipMultiplicity.Many);
var navigationProperty = entityType.AddNavigationProperty("N", associationType);
var property = EdmProperty.CreatePrimitive("Fk1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
entityType.AddMember(property);
var fkProperty1 = property;
var property1 = EdmProperty.CreatePrimitive("Fk2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
entityType.AddMember(property1);
var fkProperty2 = property1;
var foreignKeyAnnotation = new ForeignKeyAttribute("Fk2,Fk1");
navigationProperty.GetMetadataProperties().SetClrAttributes(new[] { foreignKeyAnnotation });
(new ForeignKeyNavigationPropertyAttributeConvention())
.Apply(navigationProperty, new DbModel(model, null));
Assert.NotNull(associationType.Constraint);
Assert.True(new[] { fkProperty2, fkProperty1 }.SequenceEqual(associationType.Constraint.ToProperties));
}
示例9: Apply_should_match_key_that_is_an_fk_used_in_table_splitting
public void Apply_should_match_key_that_is_an_fk_used_in_table_splitting()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = new EntityType("E", "N", DataSpace.CSpace);
var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));
entityType.AddKeyMember(property);
var targetConfig = new EntityTypeConfiguration(typeof(object));
targetConfig.ToTable("SharedTable");
entityType.GetMetadataProperties().SetConfiguration(targetConfig);
var sourceEntityType = new EntityType("E", "N", DataSpace.CSpace);
var sourceConfig = new EntityTypeConfiguration(typeof(object));
sourceConfig.ToTable("SharedTable");
sourceEntityType.GetMetadataProperties().SetConfiguration(sourceConfig);
var associationType
= model.AddAssociationType(
"A", sourceEntityType, RelationshipMultiplicity.One,
entityType, RelationshipMultiplicity.One);
associationType.Constraint
= new ReferentialConstraint(
associationType.SourceEnd,
associationType.TargetEnd,
new[] { property },
new[] { property });
(new StoreGeneratedIdentityKeyConvention())
.Apply(entityType, new DbModel(model, null));
Assert.Equal(
StoreGeneratedPattern.Identity,
entityType.KeyProperties.Single().GetStoreGeneratedPattern());
}
示例10: AddAssociationSet_should_create_and_add_to_default_container
public void AddAssociationSet_should_create_and_add_to_default_container()
{
var model = new EdmModel(DataSpace.CSpace);
var sourceEntityType = model.AddEntityType("Source");
var targetEntityType = model.AddEntityType("Target");
model.AddEntitySet("S", sourceEntityType);
model.AddEntitySet("T", targetEntityType);
var associationType = model.AddAssociationType(
"Foo",
sourceEntityType, RelationshipMultiplicity.One,
targetEntityType, RelationshipMultiplicity.Many);
var associationSet = model.AddAssociationSet("FooSet", associationType);
Assert.NotNull(associationSet);
Assert.Equal("FooSet", associationSet.Name);
Assert.Same(associationType, associationSet.ElementType);
Assert.True(model.Containers.Single().AssociationSets.Contains(associationSet));
}
示例11: Apply_should_match_optional_self_ref
public void Apply_should_match_optional_self_ref()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = new EntityType("E", "N", DataSpace.CSpace);
var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));
entityType.AddKeyMember(property);
model.AddAssociationType(
"A",
entityType, RelationshipMultiplicity.ZeroOrOne,
entityType, RelationshipMultiplicity.Many);
(new StoreGeneratedIdentityKeyConvention())
.Apply(entityType, new DbModel(model, null));
Assert.NotNull(entityType.KeyProperties.Single().GetStoreGeneratedPattern());
}
示例12: Apply_throws_with_empty_foreign_key_properties
public void Apply_throws_with_empty_foreign_key_properties()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = model.AddEntityType("E");
var mockType = new MockType();
entityType.Annotations.SetClrType(mockType);
var associationType = model.AddAssociationType(
"A",
entityType, RelationshipMultiplicity.ZeroOrOne,
entityType, RelationshipMultiplicity.Many);
var navigationProperty = entityType.AddNavigationProperty("N", associationType);
var foreignKeyAnnotation = new ForeignKeyAttribute(",");
navigationProperty.Annotations.SetClrAttributes(new[] { foreignKeyAnnotation });
Assert.Equal(
Strings.ForeignKeyAttributeConvention_EmptyKey("N", mockType.Object),
Assert.Throws<InvalidOperationException>(
() => ((IEdmConvention<NavigationProperty>)new ForeignKeyNavigationPropertyAttributeConvention())
.Apply(navigationProperty, model)).Message);
}
示例13: Apply_throws_when_cannot_find_foreign_key_properties
public void Apply_throws_when_cannot_find_foreign_key_properties()
{
var model = new EdmModel(DataSpace.CSpace);
var entityType = model.AddEntityType("E");
var mockType = new MockType();
entityType.GetMetadataProperties().SetClrType(mockType);
var associationType = model.AddAssociationType(
"A",
entityType, RelationshipMultiplicity.ZeroOrOne,
entityType, RelationshipMultiplicity.Many);
var navigationProperty = entityType.AddNavigationProperty("N", associationType);
var foreignKeyAnnotation = new ForeignKeyAttribute("_Fk");
navigationProperty.GetMetadataProperties().SetClrAttributes(new[] { foreignKeyAnnotation });
Assert.Equal(
Strings.ForeignKeyAttributeConvention_InvalidKey("N", mockType.Object, "_Fk", mockType.Object),
Assert.Throws<InvalidOperationException>(
() => (new ForeignKeyNavigationPropertyAttributeConvention())
.Apply(navigationProperty, new DbModel(model, null))).Message);
}
示例14: Generate_can_map_foreign_key_association_type
public void Generate_can_map_foreign_key_association_type()
{
var model = new EdmModel(DataSpace.CSpace);
var principalEntityType = model.AddEntityType("P");
principalEntityType.GetMetadataProperties().SetClrType(typeof(object));
var dependentEntityType = model.AddEntityType("D");
dependentEntityType.GetMetadataProperties().SetClrType(typeof(string));
var dependentProperty1 = EdmProperty.CreatePrimitive("FK1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
dependentProperty1.Nullable = false;
dependentEntityType.AddMember(dependentProperty1);
var dependentProperty2 = EdmProperty.CreatePrimitive("FK2", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
dependentEntityType.AddMember(dependentProperty2);
model.AddEntitySet("PSet", principalEntityType);
model.AddEntitySet("DSet", dependentEntityType);
var associationType
= model.AddAssociationType(
"P_D",
principalEntityType, RelationshipMultiplicity.One,
dependentEntityType, RelationshipMultiplicity.Many);
associationType.Constraint
= new ReferentialConstraint(
associationType.SourceEnd,
associationType.TargetEnd,
principalEntityType.KeyProperties,
new[] { dependentProperty1, dependentProperty2 });
associationType.SourceEnd.DeleteBehavior = OperationAction.Cascade;
var databaseMapping
= CreateDatabaseMappingGenerator().Generate(model);
var dependentTable = databaseMapping.GetEntityTypeMapping(dependentEntityType).MappingFragments.Single().Table;
var foreignKeyConstraint = dependentTable.ForeignKeyBuilders.Single();
Assert.Equal(2, dependentTable.Properties.Count());
Assert.Equal(2, foreignKeyConstraint.DependentColumns.Count());
Assert.Equal(OperationAction.Cascade, foreignKeyConstraint.DeleteAction);
Assert.Equal(associationType.Name, foreignKeyConstraint.Name);
var foreignKeyColumn = foreignKeyConstraint.DependentColumns.First();
Assert.False(foreignKeyColumn.Nullable);
Assert.Equal("FK1", foreignKeyColumn.Name);
}
示例15: GetAssociationsBetween_should_return_matching_associations
public void GetAssociationsBetween_should_return_matching_associations()
{
var model = new EdmModel(DataSpace.CSpace);
var entityTypeA = model.AddEntityType("Foo");
var entityTypeB = model.AddEntityType("Bar");
Assert.Equal(0, model.GetAssociationTypesBetween(entityTypeA, entityTypeB).Count());
model.AddAssociationType(
"Foo_Bar",
entityTypeA, RelationshipMultiplicity.ZeroOrOne,
entityTypeB, RelationshipMultiplicity.Many);
model.AddAssociationType(
"Bar_Foo",
entityTypeB, RelationshipMultiplicity.ZeroOrOne,
entityTypeA, RelationshipMultiplicity.Many);
Assert.Equal(2, model.GetAssociationTypesBetween(entityTypeA, entityTypeB).Count());
Assert.Equal(2, model.GetAssociationTypesBetween(entityTypeB, entityTypeA).Count());
Assert.Equal(0, model.GetAssociationTypesBetween(entityTypeA, entityTypeA).Count());
}