public void Query_principal_include_dependent_unidirectional() { Seed(); using (var context = new QueryFixupContext()) { var principal = context.Set <CategoryPN>().Include(e => e.Products).Single(); var dependent = principal.Products.Single(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.CategoryId); Assert.Equal(new[] { dependent }.ToList(), principal.Products); }); } }
public void Query_dependent_include_principal_unidirectional() { Seed(); using (var context = new QueryFixupContext()) { var dependent = context.Set <ProductDN>().Include(e => e.Category).Single(); var principal = dependent.Category; AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.CategoryId); Assert.Same(principal, dependent.Category); }); } }
public void Query_principal_include_dependent_self_ref() { Seed(); using var context = new QueryFixupContext(); var widgets = context.Set <Widget>().Include(e => e.ChildWidgets).ToList(); var dependent = widgets.Single(e => e.Id == 78); var principal = widgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentWidgetId); Assert.Same(principal, dependent.ParentWidget); Assert.Equal(new[] { dependent }.ToList(), principal.ChildWidgets); }); }
public void Query_principal_include_dependent_unidirectional_one_to_one() { Seed(); using (var context = new QueryFixupContext()) { var principal = context.Set <ParentPN>().Include(e => e.Child).Single(); var dependent = principal.Child; AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(dependent, principal.Child); }); } }
public void Query_dependent_include_principal_unidirectional() { Seed(); using (var context = new QueryFixupContext()) { var dependent = context.Set<ProductDN>().Include(e => e.Category).Single(); var principal = dependent.Category; AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.CategoryId); Assert.Same(principal, dependent.Category); }); } }
public void Query_subowned_foreign_key() { Seed(); using (var context = new QueryFixupContext()) { var foreignKeyValue = context.Set <Order>() .Select(o => EF.Property <int?>(o.OrderDetails.BillingAddress, "OrderDetailsId")).Single(); var principal = context.Set <Order>().AsNoTracking().Single(); AssertFixup( context, () => { Assert.Equal(principal.Id, foreignKeyValue); }); } }
public void Query_principal_include_dependent_self_ref_one_to_one() { Seed(); using var context = new QueryFixupContext(); var smidgets = context.Set <Smidget>().Include(e => e.ChildSmidget).ToList(); var dependent = smidgets.Single(e => e.Id == 78); var principal = smidgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentSmidgetId); Assert.Same(principal, dependent.ParentSmidget); Assert.Same(dependent, principal.ChildSmidget); }); }
public void Query_principal_include_dependent_unidirectional() { Seed(); using (var context = new QueryFixupContext()) { var principal = context.Set<CategoryPN>().Include(e => e.Products).Single(); var dependent = principal.Products.Single(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.CategoryId); Assert.Equal(new[] { dependent }.ToList(), principal.Products); }); } }
public void Query_subowned() { Seed(); using var context = new QueryFixupContext(); // Owned entity without owner. Issue #24807. Assert.Equal( CoreStrings.OwnedEntitiesCannotBeTrackedWithoutTheirOwner, Assert.Throws <InvalidOperationException>( () => { var subDependent1 = context.Set <Order>() .Include(a => a.OrderDetails.BillingAddress.OrderDetails.Order) .Select(o => o.OrderDetails.BillingAddress) .Single(); }).Message); // var subDependent2 = context.Set<Order>() // .Include(a => a.OrderDetails.ShippingAddress.OrderDetails.Order) // .Select(o => o.OrderDetails.ShippingAddress) // .Single(); // // AssertFixup( // context, // () => // { // Assert.Equal("BillMe", subDependent1.Street); // Assert.Equal("ShipMe", subDependent2.Street); // // var dependent = subDependent1.OrderDetails; // Assert.Same(dependent, subDependent2.OrderDetails); // Assert.NotNull(dependent.Order); // var principal = dependent.Order; // // var subDependent1Entry = context.Entry(subDependent1); // Assert.Equal(principal.Id, subDependent1Entry.Property("OrderDetailsId").CurrentValue); // Assert.Equal(typeof(OrderDetails).DisplayName() + "." + nameof(OrderDetails.BillingAddress) + "#" + typeof(Address).ShortDisplayName(), subDependent1Entry.Metadata.Name); // // var subDependent2Entry = context.Entry(subDependent2); // Assert.Equal(principal.Id, subDependent2Entry.Property("OrderDetailsId").CurrentValue); // Assert.Equal(typeof(OrderDetails).DisplayName() + "." + nameof(OrderDetails.ShippingAddress) + "#" + typeof(Address).ShortDisplayName(), subDependent2Entry.Metadata.Name); // }); }
public void Query_ownership_navigations() { Seed(); using var context = new QueryFixupContext(); var principal = context.Set <Order>() .Single(); AssertFixup( context, () => { var dependent = principal.OrderDetails; Assert.Same(principal, dependent.Order); var subDependent1 = dependent.BillingAddress; var subDependent2 = dependent.ShippingAddress; Assert.Same(dependent, subDependent1.OrderDetails); Assert.Same(dependent, subDependent2.OrderDetails); Assert.Equal("BillMe", subDependent1.Street); Assert.Equal("ShipMe", subDependent2.Street); Assert.Equal(4, context.ChangeTracker.Entries().Count()); var principalEntry = context.Entry(principal); Assert.Equal(EntityState.Unchanged, principalEntry.State); var dependentEntry = principalEntry.Reference(p => p.OrderDetails).TargetEntry; Assert.Equal(principal.Id, dependentEntry.Property("OrderId").CurrentValue); Assert.Equal(EntityState.Unchanged, dependentEntry.State); Assert.Equal(nameof(OrderDetails), dependentEntry.Metadata.FindOwnership().PrincipalToDependent.Name); var subDependent1Entry = dependentEntry.Reference(p => p.BillingAddress).TargetEntry; Assert.Equal(principal.Id, subDependent1Entry.Property("OrderDetailsId").CurrentValue); Assert.Equal(EntityState.Unchanged, subDependent1Entry.State); Assert.Equal(typeof(OrderDetails).DisplayName() + "." + nameof(OrderDetails.BillingAddress) + "#" + typeof(Address).ShortDisplayName(), subDependent1Entry.Metadata.Name); var subDependent2Entry = dependentEntry.Reference(p => p.ShippingAddress).TargetEntry; Assert.Equal(principal.Id, subDependent2Entry.Property("OrderDetailsId").CurrentValue); Assert.Equal(EntityState.Unchanged, subDependent2Entry.State); Assert.Equal(typeof(OrderDetails).DisplayName() + "." + nameof(OrderDetails.ShippingAddress) + "#" + typeof(Address).ShortDisplayName(), subDependent2Entry.Metadata.Name); }); }
public void Query_dependent_include_principal_one_to_one() { Seed(); using (var context = new QueryFixupContext()) { var dependent = context.Set <Child>().Include(e => e.Parent).Single(); var principal = dependent.Parent; AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(principal, dependent.Parent); Assert.Same(dependent, principal.Child); }); } }
public void Query_owned() { Seed(); using (var context = new QueryFixupContext()) { var owned = context.Set <Order>().Select(o => o.OrderDetails).Single(); var principal = context.Set <Order>().AsNoTracking().Single(); AssertFixup( context, () => { var dependentEntry = context.Entry(owned); Assert.Equal(principal.Id, dependentEntry.Property("OrderId").CurrentValue); Assert.Equal(nameof(Order.OrderDetails), dependentEntry.Metadata.DefiningNavigationName); }); } }
public void Query_self_ref_dependent_nav_only() { Seed(); using (var context = new QueryFixupContext()) { var widgets = context.Set <WidgetDN>().ToList(); var dependent = widgets.Single(e => e.Id == 78); var principal = widgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentWidgetId); Assert.Same(principal, dependent.ParentWidget); }); } }
public void Query_dependent_include_principal_self_ref_one_to_one_unidirectional() { Seed(); using (var context = new QueryFixupContext()) { var smidgets = context.Set <SmidgetDN>().Include(e => e.ParentSmidget).ToList(); var dependent = smidgets.Single(e => e.Id == 78); var principal = smidgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentSmidgetId); Assert.Same(principal, dependent.ParentSmidget); }); } }
public void Query_owned() { Seed(); using (var context = new QueryFixupContext()) { var owned = context.Set <Order>().Single().OrderDetails; var principal = context.Set <Order>().AsNoTracking().Single(); AssertFixup( context, () => { var dependentEntry = context.Entry(owned); Assert.Equal(principal.Id, dependentEntry.Property("OrderId").CurrentValue); Assert.Equal(nameof(Order.OrderDetails), dependentEntry.Metadata.FindOwnership().PrincipalToDependent.Name); }); } }
public void Query_principal_include_dependent_multiple_relationships() { Seed(); using var context = new QueryFixupContext(); var principal = context.Set <Blog>().Include(e => e.Posts).Single(); var dependent = principal.Posts.Single(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.BlogId); Assert.Same(principal, dependent.Blog); Assert.Equal(new[] { dependent }.ToList(), principal.Posts); Assert.Equal(dependent.Id, principal.TopPostId); Assert.Same(dependent, principal.TopPost); }); }
public void Query_dependent_include_principal_one_to_one() { Seed(); using (var context = new QueryFixupContext()) { var dependent = context.Set<Child>().Include(e => e.Parent).Single(); var principal = dependent.Parent; AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(principal, dependent.Parent); Assert.Same(dependent, principal.Child); }); } }
private static void Seed() { using (var context = new QueryFixupContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.AddRange( new Blog { Id = 77, TopPostId = 78 }, new Post { Id = 78, BlogId = 77 }, new Widget { Id = 77 }, new Widget { Id = 78, ParentWidgetId = 77 }, new WidgetPN { Id = 77 }, new WidgetPN { Id = 78, ParentWidgetId = 77 }, new WidgetDN { Id = 77 }, new WidgetDN { Id = 78, ParentWidgetId = 77 }, new Smidget { Id = 77 }, new Smidget { Id = 78, ParentSmidgetId = 77 }, new SmidgetPN { Id = 77 }, new SmidgetPN { Id = 78, ParentSmidgetId = 77 }, new SmidgetDN { Id = 77 }, new SmidgetDN { Id = 78, ParentSmidgetId = 77 }, new Category { Id = 77 }, new Product { Id = 78, CategoryId = 77 }, new CategoryPN { Id = 77 }, new ProductPN { Id = 78, CategoryId = 77 }, new CategoryDN { Id = 77 }, new ProductDN { Id = 78, CategoryId = 77 }, new Parent { Id = 77 }, new Child { Id = 78, ParentId = 77 }, new ParentPN { Id = 77 }, new ChildPN { Id = 78, ParentId = 77 }, new ParentDN { Id = 77 }, new ChildDN { Id = 78, ParentId = 77 }); context.SaveChanges(); } }
public void Query_self_ref() { Seed(); using (var context = new QueryFixupContext()) { var widgets = context.Set<Widget>().ToList(); var dependent = widgets.Single(e => e.Id == 78); var principal = widgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentWidgetId); Assert.Same(principal, dependent.ParentWidget); Assert.Equal(new[] { dependent }.ToList(), principal.ChildWidgets); }); } }
public void Query_principal_include_dependent_self_ref_unidirectional() { Seed(); using (var context = new QueryFixupContext()) { var widgets = context.Set<WidgetPN>().Include(e => e.ChildWidgets).ToList(); var dependent = widgets.Single(e => e.Id == 78); var principal = widgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentWidgetId); Assert.Equal(new[] { dependent }.ToList(), principal.ChildWidgets); }); } }
public void Query_self_ref_one_to_one_dependent_nav_only() { Seed(); using (var context = new QueryFixupContext()) { var smidgets = context.Set<SmidgetDN>().ToList(); var dependent = smidgets.Single(e => e.Id == 78); var principal = smidgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentSmidgetId); Assert.Same(principal, dependent.ParentSmidget); }); } }
public void Query_principal_include_dependent_multiple_relationsships() { Seed(); using (var context = new QueryFixupContext()) { var principal = context.Set<Blog>().Include(e => e.Posts).Single(); var dependent = principal.Posts.Single(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.BlogId); Assert.Same(principal, dependent.Blog); Assert.Equal(new[] { dependent }.ToList(), principal.Posts); Assert.Equal(dependent.Id, principal.TopPostId); Assert.Same(dependent, principal.TopPost); }); } }
public void Query_principal_include_dependent_self_ref_one_to_one_unidirectional() { Seed(); using (var context = new QueryFixupContext()) { var smidgets = context.Set<SmidgetPN>().Include(e => e.ChildSmidget).ToList(); var dependent = smidgets.Single(e => e.Id == 78); var principal = smidgets.Single(e => e.Id == 77); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentSmidgetId); Assert.Same(dependent, principal.ChildSmidget); }); } }
private static void Seed() { using var context = new QueryFixupContext(); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.AddRange( new Blog { Id = 77, TopPostId = 78 }, new Post { Id = 78, BlogId = 77 }, new Widget { Id = 77 }, new Widget { Id = 78, ParentWidgetId = 77 }, new WidgetPN { Id = 77 }, new WidgetPN { Id = 78, ParentWidgetId = 77 }, new WidgetDN { Id = 77 }, new WidgetDN { Id = 78, ParentWidgetId = 77 }, new Smidget { Id = 77 }, new Smidget { Id = 78, ParentSmidgetId = 77 }, new SmidgetPN { Id = 77 }, new SmidgetPN { Id = 78, ParentSmidgetId = 77 }, new SmidgetDN { Id = 77 }, new SmidgetDN { Id = 78, ParentSmidgetId = 77 }, new Category { Id = 77 }, new Product { Id = 78, CategoryId = 77 }, new CategoryPN { Id = 77 }, new ProductPN { Id = 78, CategoryId = 77 }, new CategoryDN { Id = 77 }, new ProductDN { Id = 78, CategoryId = 77 }, new Parent { Id = 77 }, new Child { Id = 78, ParentId = 77 }, new ParentPN { Id = 77 }, new ChildPN { Id = 78, ParentId = 77 }, new ParentDN { Id = 77 }, new ChildDN { Id = 78, ParentId = 77 }, new Order { Id = 77, OrderDetails = new OrderDetails { BillingAddress = new Address { Street = "BillMe" }, ShippingAddress = new Address { Street = "ShipMe" } } }); context.SaveChanges(); }
public void Query_principal_include_dependent_unidirectional_one_to_one() { Seed(); using (var context = new QueryFixupContext()) { var principal = context.Set<ParentPN>().Include(e => e.Child).Single(); var dependent = principal.Child; AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(dependent, principal.Child); }); } }