Skip to content

Commit

Permalink
Merge branch 'LoadRelated_CompositeKey' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Sneed committed Dec 16, 2015
2 parents 5d0e3d4 + 832201f commit 38bc454
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
<Compile Include="..\..\Tests\TrackableEntities.EF.5.Tests\NorthwindModels\Product.cs">
<Link>NorthwindModels\Product.cs</Link>
</Compile>
<Compile Include="..\..\Tests\TrackableEntities.EF.5.Tests\NorthwindModels\ProductInfo.cs">
<Link>NorthwindModels\ProductInfo.cs</Link>
</Compile>
<Compile Include="..\..\Tests\TrackableEntities.EF.5.Tests\NorthwindModels\Promo.cs">
<Link>NorthwindModels\Promo.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data.Entity;

using System.Data.Entity;
using TrackableEntities.EF.Tests.NorthwindModels;

namespace TrackableEntities.EF.Tests.Contexts
Expand Down Expand Up @@ -39,6 +40,7 @@ public NorthwindDbContext(CreateDbOptions createDbOptions = CreateDbOptions.Crea
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Promo> Promos { get; set; }
public DbSet<ProductInfo> ProductInfos { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<CustomerAddress> CustomerAddresses { get; set; }
public DbSet<CustomerSetting> CustomerSettings { get; set; }
Expand All @@ -54,6 +56,7 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
.WithOptional(x => x.CustomerSetting);
modelBuilder.Entity<Promo>().ToTable("Promos");
modelBuilder.Entity<HolidayPromo>().ToTable("HolidayPromos");
modelBuilder.Entity<ProductInfo>().ToTable("ProductInfos");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class LoadRelatedEntitiesTests
private const string TestTerritoryId1 = "11111";
private const string TestTerritoryId2 = "22222";
private const string TestTerritoryId3 = "33333";
private const int ProductInfo1A = 1;
private const int ProductInfo1B = 2;
private const int ProductInfo2A = 1;
private const int ProductInfo2B = 3;
private const CreateDbOptions CreateNorthwindDbOptions = CreateDbOptions.DropCreateDatabaseIfModelChanges;

#region Setup
Expand All @@ -53,6 +57,10 @@ public LoadRelatedEntitiesTests()
EnsureTestTerritory(context, TestTerritoryId2);
EnsureTestTerritory(context, TestTerritoryId3);

// Test Product Infos
EnsureTestProductInfo(context, ProductInfo1A, ProductInfo1B);
EnsureTestProductInfo(context, ProductInfo2A, ProductInfo2B);

// Save changes
context.SaveChanges();
}
Expand Down Expand Up @@ -96,6 +104,23 @@ private static void EnsureTestCustomerSetting(NorthwindDbContext context, string
}
}

private static void EnsureTestProductInfo(NorthwindDbContext context, int productInfo1, int productInfo2)
{
var info = context.ProductInfos
.SingleOrDefault(pi => pi.ProductInfoKey1 == productInfo1
&& pi.ProductInfoKey2 == productInfo2);
if (info == null)
{
info = new ProductInfo
{
ProductInfoKey1 = productInfo1,
ProductInfoKey2 = productInfo2,
Info = "Test Product Info"
};
context.ProductInfos.Add(info);
}
}

private List<Order> CreateTestOrders(NorthwindDbContext context)
{
// Create test entities
Expand Down Expand Up @@ -273,6 +298,40 @@ private List<Product> CreateTestProductsWithPromos(NorthwindDbContext context)
return new List<Product> { product1 };
}

private List<Product> CreateTestProductsWithProductInfo(NorthwindDbContext context)
{
// Create test entities
var category1 = new Category
{
CategoryName = "Test Category 1b"
};
var info1 = context.ProductInfos
.Single(pi => pi.ProductInfoKey1 == ProductInfo1A
&& pi.ProductInfoKey2 == ProductInfo1B);
var product1 = new Product
{
ProductName = "Test Product 1b",
UnitPrice = 10M,
Category = category1,
ProductInfo = info1
};

// Persist entities
context.Products.Add(product1);
context.SaveChanges();

// Detach entities
var objContext = ((IObjectContextAdapter)context).ObjectContext;
objContext.Detach(product1);

// Clear reference properties
product1.Category = null;
product1.ProductInfo = null;

// Return entities
return new List<Product> { product1 };
}

#endregion

#region Order-Customer: Many-to-One
Expand Down Expand Up @@ -324,7 +383,9 @@ public void LoadRelatedEntities_Should_Populate_Multiple_Orders_With_Customer()
Assert.False(orders.Any(o => o.Customer.CustomerId != o.CustomerId));
}

[Fact(Skip = "NotSupportedException: Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns.")]
// Sometimes fails with NotSupportedException for EF6:
// DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility.
/* [Fact]
public void Edmx_LoadRelatedEntities_Should_Populate_Multiple_Orders_With_Customer()
{
// Create DB usng CodeFirst context
Expand Down Expand Up @@ -358,7 +419,7 @@ public void Edmx_LoadRelatedEntities_Should_Populate_Multiple_Orders_With_Custom
// Assert
Assert.False(orders.Any(o => o.Customer == null));
Assert.False(orders.Any(o => o.Customer.CustomerId != o.CustomerId));
}
} */

[Fact]
public void LoadRelatedEntities_Should_Populate_Order_With_Customer_With_Territory()
Expand Down Expand Up @@ -498,6 +559,27 @@ public void LoadRelatedEntities_Should_Populate_Product_With_HolidayPromo()

#endregion

#region Product-ProductInfo: Reference-with-CompositeKey

[Fact]
public void LoadRelatedEntities_Should_Populate_Product_With_ProductInfo()
{
// Arrange
var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
var product = CreateTestProductsWithProductInfo(context)[0];
product.TrackingState = TrackingState.Added;

// Act
context.LoadRelatedEntities(product);

// Assert
Assert.NotNull(product.ProductInfo);
Assert.Equal(product.ProductInfoKey1, product.ProductInfo.ProductInfoKey1);
Assert.Equal(product.ProductInfoKey2, product.ProductInfo.ProductInfoKey2);
}

#endregion

#region Order-OrderDetail-Product-Category: One-to-Many-to-One

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ public partial class Product : ITrackable
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public bool Discontinued { get; set; }

public int CategoryId { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }

public int? PromoId { get; set; }
[ForeignKey("PromoId")]
public HolidayPromo HolidayPromo { get; set; }

[ForeignKey("ProductInfo"), Column(Order = 1)]
public int? ProductInfoKey1 { get; set; }
[ForeignKey("ProductInfo"), Column(Order = 2)]
public int? ProductInfoKey2 { get; set; }
public ProductInfo ProductInfo { get; set; }

[NotMapped]
public TrackingState TrackingState { get; set; }
[NotMapped]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace TrackableEntities.EF.Tests.NorthwindModels
{
public partial class ProductInfo : ITrackable
{
[Key, Column(Order = 1)]
public int ProductInfoKey1 { get; set; }
[Key, Column(Order = 2)]
public int ProductInfoKey2 { get; set; }

public string Info { get; set; }

[NotMapped]
public TrackingState TrackingState { get; set; }
[NotMapped]
public ICollection<string> ModifiedProperties { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Mocks\MockNorthwind.cs" />
<Compile Include="NorthwindDbInitializer.cs" />
<Compile Include="NorthwindModels\HolidayPromo.cs" />
<Compile Include="NorthwindModels\ProductInfo.cs" />
<Compile Include="NorthwindModels\Promo.cs" />
<Compile Include="NorthwindModels\CustomerAddress.cs" />
<Compile Include="NorthwindModels\Area.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
<Compile Include="..\TrackableEntities.EF.5.Tests\NorthwindModels\Product.cs">
<Link>NorthwindModels\Product.cs</Link>
</Compile>
<Compile Include="..\TrackableEntities.EF.5.Tests\NorthwindModels\ProductInfo.cs">
<Link>NorthwindModels\ProductInfo.cs</Link>
</Compile>
<Compile Include="..\TrackableEntities.EF.5.Tests\NorthwindModels\Promo.cs">
<Link>NorthwindModels\Promo.cs</Link>
</Compile>
Expand Down
Loading

0 comments on commit 38bc454

Please sign in to comment.