本文整理汇总了C#中EntityType.GetOrAddKey方法的典型用法代码示例。如果您正苦于以下问题:C# EntityType.GetOrAddKey方法的具体用法?C# EntityType.GetOrAddKey怎么用?C# EntityType.GetOrAddKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EntityType
的用法示例。
在下文中一共展示了EntityType.GetOrAddKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Adding_a_new_navigation_with_a_name_that_already_exists_throws
public void Adding_a_new_navigation_with_a_name_that_already_exists_throws()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
orderType.AddNavigation("Customer", customerForeignKey, pointsToPrincipal: true);
Assert.Equal(
Strings.DuplicateNavigation("Customer", typeof(Order).FullName),
Assert.Throws<InvalidOperationException>(
() => orderType.AddNavigation("Customer", customerForeignKey, pointsToPrincipal: true)).Message);
}
示例2: Can_add_new_navigations_or_get_existing_navigations
public void Can_add_new_navigations_or_get_existing_navigations()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
var customerNavigation = orderType.GetOrAddNavigation("Customer", customerForeignKey, pointsToPrincipal: true);
Assert.Equal("Customer", customerNavigation.Name);
Assert.Same(orderType, customerNavigation.EntityType);
Assert.Same(customerForeignKey, customerNavigation.ForeignKey);
Assert.True(customerNavigation.PointsToPrincipal);
Assert.False(customerNavigation.IsCollection());
Assert.Same(customerType, customerNavigation.GetTargetType());
Assert.Same(customerNavigation, orderType.GetOrAddNavigation("Customer", customerForeignKey, pointsToPrincipal: false));
Assert.True(customerNavigation.PointsToPrincipal);
}
示例3: Can_get_navigation_and_can_try_get_navigation
public void Can_get_navigation_and_can_try_get_navigation()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
var customerNavigation = orderType.GetOrAddNavigation("Customer", customerForeignKey, pointsToPrincipal: true);
Assert.Same(customerNavigation, orderType.FindNavigation("Customer"));
Assert.Same(customerNavigation, orderType.GetNavigation("Customer"));
Assert.Null(orderType.FindNavigation("Nose"));
Assert.Equal(
Strings.NavigationNotFound("Nose", typeof(Order).FullName),
Assert.Throws<ModelItemNotFoundException>(() => orderType.GetNavigation("Nose")).Message);
}
示例4: Cannot_remove_property_when_used_by_non_primary_key
public void Cannot_remove_property_when_used_by_non_primary_key()
{
var entityType = new EntityType(typeof(Customer), new Model());
var property = entityType.GetOrAddProperty(Customer.IdProperty);
entityType.GetOrAddKey(property);
Assert.Equal(
Strings.PropertyInUse("Id", typeof(Customer).FullName),
Assert.Throws<InvalidOperationException>(() => entityType.RemoveProperty(property)).Message);
}
示例5: Can_add_and_remove_navigations
public void Can_add_and_remove_navigations()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
Assert.Null(orderType.RemoveNavigation(new Navigation("Customer", customerForeignKey, pointsToPrincipal: true)));
var customerNavigation = orderType.AddNavigation("Customer", customerForeignKey, pointsToPrincipal: true);
var ordersNavigation = customerType.AddNavigation("Orders", customerForeignKey, pointsToPrincipal: false);
Assert.Equal("Customer", customerNavigation.Name);
Assert.Same(orderType, customerNavigation.EntityType);
Assert.Same(customerForeignKey, customerNavigation.ForeignKey);
Assert.True(customerNavigation.PointsToPrincipal);
Assert.False(customerNavigation.IsCollection());
Assert.Same(customerType, customerNavigation.GetTargetType());
Assert.Same(customerNavigation, customerForeignKey.GetNavigationToPrincipal());
Assert.Equal("Orders", ordersNavigation.Name);
Assert.Same(customerType, ordersNavigation.EntityType);
Assert.Same(customerForeignKey, ordersNavigation.ForeignKey);
Assert.False(ordersNavigation.PointsToPrincipal);
Assert.True(ordersNavigation.IsCollection());
Assert.Same(orderType, ordersNavigation.GetTargetType());
Assert.Same(ordersNavigation, customerForeignKey.GetNavigationToDependent());
Assert.Same(customerNavigation, orderType.Navigations.Single());
Assert.Same(ordersNavigation, customerType.Navigations.Single());
Assert.Same(customerNavigation, orderType.RemoveNavigation(customerNavigation));
Assert.Null(orderType.RemoveNavigation(customerNavigation));
Assert.Empty(orderType.Navigations);
Assert.Same(ordersNavigation, customerType.RemoveNavigation(new Navigation("Orders", customerForeignKey, pointsToPrincipal: false)));
Assert.Empty(customerType.Navigations);
}
示例6: Adding_a_navigation_that_doesnt_match_a_CLR_property_throws
public void Adding_a_navigation_that_doesnt_match_a_CLR_property_throws()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
Assert.Equal(
Strings.NoClrNavigation("Snook", typeof(Order).FullName),
Assert.Throws<InvalidOperationException>(
() => orderType.AddNavigation("Snook", customerForeignKey, pointsToPrincipal: true)).Message);
}
示例7: Reference_navigation_properties_must_be_of_the_target_type
public void Reference_navigation_properties_must_be_of_the_target_type()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
Assert.Equal(
Strings.NavigationSingleWrongClrType("OrderCustomer", typeof(Order).FullName, typeof(Order).FullName, typeof(Customer).FullName),
Assert.Throws<InvalidOperationException>(
() => orderType.AddNavigation("OrderCustomer", customerForeignKey, pointsToPrincipal: true)).Message);
Assert.Equal(
Strings.NavigationSingleWrongClrType("DerivedCustomer", typeof(Order).FullName, typeof(SpecialCustomer).FullName, typeof(Customer).FullName),
Assert.Throws<InvalidOperationException>(
() => orderType.AddNavigation("DerivedCustomer", customerForeignKey, pointsToPrincipal: true)).Message);
}
示例8: Adding_a_foreign_key_throws_if_duplicate
public void Adding_a_foreign_key_throws_if_duplicate()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var customerFk1 = orderType.GetOrAddProperty(Order.CustomerIdProperty);
orderType.AddForeignKey(customerFk1, customerKey);
Assert.Equal(
Strings.DuplicateForeignKey("{'" + Order.CustomerIdProperty.Name + "'}", typeof(Order).FullName),
Assert.Throws<InvalidOperationException>(() => orderType.AddForeignKey(customerFk1, customerKey)).Message);
}
示例9: Adding_a_foreign_key_throws_if_properties_from_different_type
public void Adding_a_foreign_key_throws_if_properties_from_different_type()
{
var entityType1 = new EntityType(typeof(Customer), new Model());
var entityType2 = new EntityType(typeof(Order), new Model());
var idProperty = entityType2.GetOrAddProperty(Customer.IdProperty);
Assert.Equal(
Strings.ForeignKeyPropertiesWrongEntity("{'" + Customer.IdProperty.Name + "'}", typeof(Customer).FullName),
Assert.Throws<ArgumentException>(() => entityType1.AddForeignKey(new[] { idProperty }, entityType2.GetOrAddKey(idProperty))).Message);
}
示例10: Keys_are_ordered_by_property_count_then_property_names
public void Keys_are_ordered_by_property_count_then_property_names()
{
var customerType = new EntityType(typeof(Customer), new Model());
var idProperty = customerType.GetOrAddProperty(Customer.IdProperty);
var nameProperty = customerType.GetOrAddProperty(Customer.NameProperty);
var otherNameProperty = customerType.GetOrAddProperty("OtherNameProperty", typeof(string), shadowProperty: true);
var k2 = customerType.GetOrAddKey(nameProperty);
var k4 = customerType.GetOrAddKey(new[] { idProperty, otherNameProperty });
var k3 = customerType.GetOrAddKey(new[] { idProperty, nameProperty });
var k1 = customerType.GetOrAddKey(idProperty);
Assert.True(new[] { k1, k2, k3, k4 }.SequenceEqual(customerType.Keys));
}
示例11: Key_properties_are_always_read_only
public void Key_properties_are_always_read_only()
{
var entityType = new EntityType(typeof(Customer), new Model());
var idProperty = entityType.GetOrAddProperty(Customer.IdProperty);
var nameProperty = entityType.GetOrAddProperty(Customer.NameProperty);
Assert.False(((IProperty)idProperty).IsReadOnly);
Assert.False(((IProperty)nameProperty).IsReadOnly);
entityType.GetOrAddKey(new[] { idProperty, nameProperty });
Assert.True(((IProperty)idProperty).IsReadOnly);
Assert.True(((IProperty)nameProperty).IsReadOnly);
nameProperty.IsReadOnly = true;
Assert.Equal(
Strings.KeyPropertyMustBeReadOnly(Customer.NameProperty.Name, typeof(Customer).FullName),
Assert.Throws<NotSupportedException>(() => nameProperty.IsReadOnly = false).Message);
Assert.True(((IProperty)idProperty).IsReadOnly);
Assert.True(((IProperty)nameProperty).IsReadOnly);
}
示例12: Can_remove_keys
public void Can_remove_keys()
{
var entityType = new EntityType(typeof(Customer), new Model());
var idProperty = entityType.GetOrAddProperty(Customer.IdProperty);
var nameProperty = entityType.GetOrAddProperty(Customer.NameProperty);
Assert.Equal(
Strings.KeyNotFound("{'" + idProperty.Name + "', '" + nameProperty.Name + "'}", typeof(Customer).FullName),
Assert.Throws<ModelItemNotFoundException>(() => entityType.GetKey(new[] { idProperty, nameProperty })).Message);
Assert.Null(entityType.RemoveKey(new Key(new[] { idProperty })));
var key1 = entityType.GetOrSetPrimaryKey(new[] { idProperty, nameProperty });
var key2 = entityType.GetOrAddKey(idProperty);
Assert.Equal(new[] { key2, key1 }, entityType.Keys.ToArray());
Assert.Same(key1, entityType.RemoveKey(key1));
Assert.Null(entityType.RemoveKey(key1));
Assert.Equal(
Strings.KeyNotFound("{'" + idProperty.Name + "', '" + nameProperty.Name + "'}", typeof(Customer).FullName),
Assert.Throws<ModelItemNotFoundException>(() => entityType.GetKey(new[] { idProperty, nameProperty })).Message);
Assert.Equal(new[] { key2 }, entityType.Keys.ToArray());
Assert.Same(key2, entityType.RemoveKey(new Key(new[] { idProperty })));
Assert.Empty(entityType.Keys);
}
示例13: Adding_a_key_throws_if_duplicated
public void Adding_a_key_throws_if_duplicated()
{
var entityType = new EntityType(typeof(Customer), new Model());
var idProperty = entityType.GetOrAddProperty(Customer.IdProperty);
var nameProperty = entityType.GetOrAddProperty(Customer.NameProperty);
entityType.GetOrAddKey(new[] { idProperty, nameProperty });
Assert.Equal(
Strings.DuplicateKey("{'" + Customer.IdProperty.Name + "', '" + Customer.NameProperty.Name + "'}", typeof(Customer).FullName),
Assert.Throws<InvalidOperationException>(() => entityType.AddKey(new[] { idProperty, nameProperty })).Message);
}
示例14: Can_add_and_get_a_key
public void Can_add_and_get_a_key()
{
var entityType = new EntityType(typeof(Customer), new Model());
var idProperty = entityType.GetOrAddProperty(Customer.IdProperty);
var nameProperty = entityType.GetOrAddProperty(Customer.NameProperty);
var key1 = entityType.AddKey(new[] { idProperty, nameProperty });
Assert.NotNull(key1);
Assert.Same(key1, entityType.GetOrAddKey(new[] { idProperty, nameProperty }));
Assert.Same(key1, entityType.Keys.Single());
var key2 = entityType.GetOrAddKey(idProperty);
Assert.NotNull(key2);
Assert.Same(key2, entityType.GetKey(idProperty));
Assert.Equal(2, entityType.Keys.Count());
Assert.Contains(key1, entityType.Keys);
Assert.Contains(key2, entityType.Keys);
}
示例15: Adding_a_navigation_that_belongs_to_a_different_type_throws
public void Adding_a_navigation_that_belongs_to_a_different_type_throws()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
Assert.Equal(
Strings.NavigationAlreadyOwned("Customer", typeof(Customer).FullName, typeof(Order).FullName),
Assert.Throws<InvalidOperationException>(() => customerType.AddNavigation("Customer", customerForeignKey, pointsToPrincipal: true)).Message);
}