本文整理汇总了C#中EntityType.GetOrAddForeignKey方法的典型用法代码示例。如果您正苦于以下问题:C# EntityType.GetOrAddForeignKey方法的具体用法?C# EntityType.GetOrAddForeignKey怎么用?C# EntityType.GetOrAddForeignKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EntityType
的用法示例。
在下文中一共展示了EntityType.GetOrAddForeignKey方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: Multiple_sets_of_navigations_using_the_same_foreign_key_are_not_allowed
public void Multiple_sets_of_navigations_using_the_same_foreign_key_are_not_allowed()
{
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);
customerType.AddNavigation("EnumerableOrders", customerForeignKey, pointsToPrincipal: false);
Assert.Equal(
Strings.MultipleNavigations("Orders", "EnumerableOrders", typeof(Customer).FullName),
Assert.Throws<InvalidOperationException>(
() => customerType.AddNavigation("Orders", customerForeignKey, pointsToPrincipal: false)).Message);
}
示例3: 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);
}
示例4: Collection_navigation_properties_must_be_IEnumerables_of_the_target_type
public void Collection_navigation_properties_must_be_IEnumerables_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.NavigationCollectionWrongClrType("NotCollectionOrders", typeof(Customer).FullName, typeof(Order).FullName, typeof(Order).FullName),
Assert.Throws<InvalidOperationException>(
() => customerType.AddNavigation("NotCollectionOrders", customerForeignKey, pointsToPrincipal: false)).Message);
Assert.Equal(
Strings.NavigationCollectionWrongClrType("DerivedOrders", typeof(Customer).FullName, typeof(IEnumerable<SpecialOrder>).FullName, typeof(Order).FullName),
Assert.Throws<InvalidOperationException>(
() => customerType.AddNavigation("DerivedOrders", customerForeignKey, pointsToPrincipal: false)).Message);
}
示例5: 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);
}
示例6: Adding_a_navigation_pointing_to_a_shadow_entity_type_throws
public void Adding_a_navigation_pointing_to_a_shadow_entity_type_throws()
{
var customerType = new EntityType("Customer", new Model());
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty("Id", typeof(int), shadowProperty: true));
var orderType = new EntityType(typeof(Order), new Model());
var foreignKeyProperty = orderType.GetOrAddProperty("CustomerId", typeof(int), shadowProperty: true);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey);
Assert.Equal(
Strings.NavigationToShadowEntity("Customer", typeof(Order).FullName, "Customer"),
Assert.Throws<InvalidOperationException>(
() => orderType.AddNavigation("Customer", customerForeignKey, pointsToPrincipal: true)).Message);
}
示例7: 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);
}
示例8: 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);
}
示例9: 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);
}
示例10: 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);
}
示例11: Can_get_or_add_a_foreign_key
public void Can_get_or_add_a_foreign_key()
{
var customerType = new EntityType(typeof(Customer), new Model());
var idProperty = customerType.GetOrAddProperty(Customer.IdProperty);
var customerKey = customerType.GetOrAddKey(idProperty);
var orderType = new EntityType(typeof(Order), new Model());
var customerFk1 = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerFk2 = orderType.GetOrAddProperty("IdAgain", typeof(int), shadowProperty: true);
var fk1 = orderType.AddForeignKey(customerFk1, customerKey);
var fk2 = orderType.GetOrAddForeignKey(customerFk2, customerKey);
Assert.NotNull(fk2);
Assert.NotEqual(fk1, fk2);
Assert.Same(fk2, orderType.GetForeignKey(customerFk2));
Assert.Same(fk2, orderType.FindForeignKey(customerFk2));
Assert.Equal(new[] { fk1, fk2 }, orderType.ForeignKeys.ToArray());
Assert.Same(fk2, orderType.GetOrAddForeignKey(customerFk2, customerKey));
Assert.Equal(new[] { fk1, fk2 }, orderType.ForeignKeys.ToArray());
}
示例12: FK_properties_are_marked_as_requiring_original_values
public void FK_properties_are_marked_as_requiring_original_values()
{
var entityType = new EntityType(typeof(FullNotificationEntity), new Model());
entityType.GetOrSetPrimaryKey(entityType.GetOrAddProperty("Id", typeof(int)));
Assert.Equal(-1, entityType.GetProperty("Id").GetOriginalValueIndex());
entityType.GetOrAddForeignKey(new[] { entityType.GetOrAddProperty("Id", typeof(int)) }, entityType.GetPrimaryKey());
Assert.Equal(0, entityType.GetProperty("Id").GetOriginalValueIndex());
}
示例13: Cannot_remove_property_when_used_by_foreign_key
public void Cannot_remove_property_when_used_by_foreign_key()
{
var customerType = new EntityType(typeof(Customer), new Model());
var customerPk = customerType.GetOrSetPrimaryKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = new EntityType(typeof(Order), new Model());
var customerFk = orderType.GetOrAddProperty(Order.CustomerIdProperty);
orderType.GetOrAddForeignKey(customerFk, customerPk);
Assert.Equal(
Strings.PropertyInUse("CustomerId", typeof(Order).FullName),
Assert.Throws<InvalidOperationException>(() => orderType.RemoveProperty(customerFk)).Message);
}