Skip to content

Commit

Permalink
Added support for OwnedEntity cloning
Browse files Browse the repository at this point in the history
Updated Microsoft.EntityFrameworkCore to version 3.1.4
Updated this package to version 0.0.4
  • Loading branch information
Henk Kin authored and Henk Kin committed May 27, 2020
1 parent 99b2490 commit 51f6d9e
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ public class CloneByIdWithIncludesIntegrationTests : DbContextTestBase

public CloneByIdWithIncludesIntegrationTests() : base(nameof(CloneByIdWithIncludesIntegrationTests))
{
var country = new Country
{
Name = "Netherlands"
};
_customer = new Customer
{
RowVersion = new[] { byte.MinValue },
BirthDate = _birthDate,
Address = new Address
{
HouseNumber = 25,
Street = "Street"
Street = "Street",
Country = country
},
Orders = new List<Order>
{
Expand All @@ -41,13 +46,22 @@ public CloneByIdWithIncludesIntegrationTests() : base(nameof(CloneByIdWithInclud
OrderDate = _orderDate,
OrderStatus = OrderStatus.Order,
TenantId = 1,
InstallationAddress = new Address
{
HouseNumber = 25,
Street = "Street",
Country = country
},
TotalOrderPrice = new Money{ Amount = 1000m, Currency = "EUR"},
OrderLines = new List<OrderLine>
{
new OrderLine
{
Quantity = 1,
UnitPrice = new Money{ Amount = 500m, Currency = "EUR"},
Article = new Article
{

ArticleTranslations = new List<ArticleTranslation>
{
new ArticleTranslation
Expand All @@ -66,6 +80,7 @@ public CloneByIdWithIncludesIntegrationTests() : base(nameof(CloneByIdWithInclud
new OrderLine
{
Quantity = 2,
UnitPrice = new Money{ Amount = 250m, Currency = "EUR"},
Article = new Article
{
ArticleTranslations = new List<ArticleTranslation>
Expand Down Expand Up @@ -109,6 +124,10 @@ public async Task Customer_IncludeEntityWithIncludeForOwnsEntityAddress()
// Act
var clone = await TestDbContext.CloneAsync<Customer>(x => x
.Include(c => c.Address)
//.ThenInclude(c => c.Country)
.Include(c => c.Orders)
.ThenInclude(c => c.InstallationAddress)
//.ThenInclude(c => c.Country)
, _customer.Id);

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public CloneEnumerableIntegrationTests() : base(nameof(CloneEnumerableIntegratio
Address = new Address
{
HouseNumber = 25,
Street = "Street"
Street = "Street",
Country = new Country
{
Name = "Netherlands"
}
},
Orders = new List<Order>
{
Expand All @@ -46,6 +50,15 @@ public CloneEnumerableIntegrationTests() : base(nameof(CloneEnumerableIntegratio
OrderDate = _orderDate,
OrderStatus = OrderStatus.Order,
TenantId = 1,
InstallationAddress = new Address
{
HouseNumber = 25,
Street = "Street",
Country = new Country
{
Name = "Netherlands"
}
},
OrderLines = new List<OrderLine>
{
new OrderLine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="Moq.AutoMock" Version="1.2.0.120" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="Moq.AutoMock" Version="2.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public class Address
{
public string Street { get; set; }
public int HouseNumber { get; set; }
public Country Country { get; set; }
public int CountryId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EntityCloner.Microsoft.EntityFrameworkCore.Tests.TestModels
{


public class Country
{
public int Id { get; set; }
public byte[] RowVersion { get; set; }
public string Name{ get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Customer
public byte[] RowVersion { get; set; }
public DateTime BirthDate { get; set; }
public Address Address { get; set; }

public ICollection<Order> Orders { get; set; } = new List<Order>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace EntityCloner.Microsoft.EntityFrameworkCore.Tests.TestModels
{
public class Money
{
public string Currency { get; set; }
public decimal Amount { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ public class Order
public DateTime OfferDate { get; set; }
public DateTime? OrderDate { get; set; }
public OrderStatus OrderStatus { get; set; }
public Address InstallationAddress { get; set; }
public bool IsDeleted { get; set; }
public int TenantId { get; set; }
public string Description { get; set; }
public Money TotalOrderPrice { get; set; }
public ICollection<OrderLine> OrderLines { get; set; } = new List<OrderLine>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class OrderLine
public int OrderId { get; set; }
public Order Order { get; set; }
public decimal Quantity { get; set; }
public Money UnitPrice { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,43 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Customer>().HasKey(x => x.Id);
modelBuilder.Entity<Customer>().Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Customer>().Property(x => x.RowVersion).IsRowVersion();
modelBuilder.Entity<Customer>().OwnsOne(x => x.Address);
modelBuilder.Entity<Customer>().OwnsOne(x => x.Address, addressBuilder=>
{
addressBuilder.Property(x => x.CountryId).IsRequired();
addressBuilder.HasOne(a => a.Country)
.WithMany()
.HasForeignKey(address => address.CountryId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
});


modelBuilder.Entity<Order>().HasKey(x => x.Id);
modelBuilder.Entity<Order>().Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Order>().Property(x => x.RowVersion).IsRowVersion();
modelBuilder.Entity<Order>().OwnsOne(x => x.InstallationAddress, addressBuilder =>
{
addressBuilder.Property(x => x.CountryId).IsRequired();
addressBuilder.HasOne(a => a.Country)
.WithMany()
.HasForeignKey(address => address.CountryId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity<Order>().OwnsOne(x=>x.TotalOrderPrice, moneyBuilder =>
{
moneyBuilder.Property(e => e.Amount).IsRequired();
moneyBuilder.Property(e => e.Currency).IsRequired();
});

modelBuilder.Entity<OrderLine>().HasKey(x => x.Id);
modelBuilder.Entity<OrderLine>().Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<OrderLine>().Property(x => x.RowVersion).IsRowVersion();

modelBuilder.Entity<OrderLine>().OwnsOne(x => x.UnitPrice, moneyBuilder =>
{
moneyBuilder.Property(e => e.Amount).IsRequired();
moneyBuilder.Property(e => e.Currency).IsRequired();
});
modelBuilder.Entity<Article>().HasKey(x => x.Id);
modelBuilder.Entity<Article>().Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Article>().Property(x => x.RowVersion).IsRowVersion();
Expand Down
Loading

0 comments on commit 51f6d9e

Please sign in to comment.