From 33f18a1ea3d1f9af44a0ec6067e86df51fc2a3cd Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Sun, 25 Nov 2018 14:51:53 +0100 Subject: [PATCH 01/11] Update readme --- README.md | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 88c4a52..7c86778 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,41 @@ AutoMapper -AutoMapper.Collection.EFCore -================================ +# AutoMapper.Collection.EntityFrameworkCore + `Automapper.Collection.EntityFrameworkCore` will help you mapping of EntityFrameowrk Core DbContext-object. - - Mapper.Initialize(cfg => - { - cfg.AddCollectionMappers(); - cfg.SetGeneratePropertyMaps>(); - // Configuration code - }); -User defined equality expressions will overwrite primary key expressions. +## Configuration examples + +- Usage together with Dependency injection and AutoMapper.Extensions.Microsoft.DependencyInjection pacakge + ``` + var services = new ServiceCollection(); + services + .AddEntityFrameworkInMemoryDatabase() + .AddDbContext(); + + services.AddAutoMapper(automapper => + { + automapper.AddCollectionMappers(); + automapper.UseEntityFrameworkCoreModel(services); + }, typeof(DB).Assembly); + + var serviceProvider = services.BuildServiceProvider(); + ``` + +- Simple usage with static version of AutoMapper + ``` + Mapper.Initialize(x => + { + x.AddCollectionMappers(); + x.UseEntityFrameworkCoreModel(); + }); + ``` + +**Note:** User defined equality expressions will overwrite primary key expressions. What about comparing to a single existing Entity for updating? -------------------------------- -Automapper.Collection.EntityFramework does that as well through extension method from of DbSet. +Automapper.Collection.EntityFrameworkCore does that as well through extension method from of DbSet. Translate equality between dto and EF object to an expression of just the EF using the dto's values as constants. From f2c00bf2d0fb35711337b63a09b51a22720d9621 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Sun, 25 Nov 2018 14:55:20 +0100 Subject: [PATCH 02/11] Update error message --- .../GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs b/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs index 8a77764..584f4d0 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs @@ -14,7 +14,7 @@ public class GenerateEntityFrameworkCorePrimaryKeyPropertyMaps public GenerateEntityFrameworkCorePrimaryKeyPropertyMaps() { - throw new InvalidOperationException("Use AddEntityFrameworkCoreKeys instead of using SetGeneratePropertyMaps."); + throw new InvalidOperationException($"Use {nameof(MapperConfigurationExpressionExtensions.UseEntityFrameworkCoreModel)} instead of using SetGeneratePropertyMaps."); } public GenerateEntityFrameworkCorePrimaryKeyPropertyMaps(IModel model) => _model = model; From a3eddfbd946855f66b32b90faad0dde5dfda1956 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Fri, 26 Apr 2019 23:45:51 +0200 Subject: [PATCH 03/11] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c86778..dca2d6a 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,10 @@ .AddEntityFrameworkInMemoryDatabase() .AddDbContext(); - services.AddAutoMapper(automapper => + services.AddAutoMapper((serviceProvider, automapper) => { automapper.AddCollectionMappers(); - automapper.UseEntityFrameworkCoreModel(services); + automapper.UseEntityFrameworkCoreModel(serviceProvider); }, typeof(DB).Assembly); var serviceProvider = services.BuildServiceProvider(); From d3968e9f96ecdaed8c81d24c68d3f0d7e9536803 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Tue, 13 Aug 2019 11:36:24 +0200 Subject: [PATCH 04/11] Ensure /NoLogo is the last command line parameter, otherwise the build is failing with some dotnet versions --- build.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.ps1 b/build.ps1 index 468a580..2d198f4 100644 --- a/build.ps1 +++ b/build.ps1 @@ -49,7 +49,7 @@ task compile -depends clean { # restore all project references (creating project.assets.json for each project) exec { dotnet restore $base_dir\AutoMapper.Collection.EFCore.sln /nologo } - exec { dotnet build $base_dir\AutoMapper.Collection.EFCore.sln -c $config $buildParam /nologo --no-restore } + exec { dotnet build $base_dir\AutoMapper.Collection.EFCore.sln -c $config $buildParam --no-restore /nologo } exec { dotnet pack $base_dir\AutoMapper.Collection.EFCore.sln -c $config --include-symbols --no-build --no-restore --output $artifacts_dir $packageParam /nologo} From d9cf996de114f75785d8d511b45c546a63df61d3 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Tue, 13 Aug 2019 11:37:10 +0200 Subject: [PATCH 05/11] Update to AM.Collection 6 and AM 9 --- ...AutoMapper.Collection.EntityFrameworkCore.csproj | 2 +- .../Extensions.cs | 4 +++- .../Persistence.cs | 13 ++++--------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj b/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj index afc7f8d..cbc646f 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj +++ b/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs b/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs index bc50cb3..ad4e004 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs @@ -9,16 +9,18 @@ namespace AutoMapper.EntityFrameworkCore public static class Extensions { /// + /// Obsolete: Use Persist(IMapper) instead. /// Create a Persistence object for the to have data persisted or removed from /// Uses static API's Mapper for finding TypeMap between classes /// /// Source table type to be updated /// DbSet to be updated /// Persistence object to Update or Remove data + [Obsolete("Use Persist(IMapper) instead.", true)] public static IPersistence Persist(this DbSet source) where TSource : class { - return new Persistence(source, Mapper.Instance); + throw new NotSupportedException(); } /// diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs b/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs index afd77a9..903e78c 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs @@ -16,7 +16,7 @@ public class Persistence : IPersistence public Persistence(DbSet sourceSet, IMapper mapper) { _sourceSet = sourceSet; - _mapper = mapper; + _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); } public TTo InsertOrUpdate(TFrom from) @@ -85,15 +85,12 @@ private TTo MapObject(Type type, object from, TTo to) { if (to == null) { - to = (TTo)(_mapper?.Map(from, type, typeof(TTo)) ?? Mapper.Map(from, type, typeof(TTo))); + to = (TTo)_mapper.Map(from, type, typeof(TTo)); _sourceSet.Add(to); } else { - if (_mapper == null) - Mapper.Map(from, to); - else - _mapper.Map(from, to); + _mapper.Map(from, to); } return to; } @@ -105,9 +102,7 @@ private Expression> GetEquivalenceExpression(TFrom from) private Expression> GetEquivalenceExpression(Type type, object from) { - return _mapper == null - ? Mapper.Map(from, type, typeof(Expression>)) as Expression> - : _mapper.Map(from, type, typeof(Expression>)) as Expression>; + return _mapper.Map(from, type, typeof(Expression>)) as Expression>; } } } \ No newline at end of file From 4c8782694105fb9e73b61cb40d4f1ada67c5ff2a Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Tue, 13 Aug 2019 11:38:28 +0200 Subject: [PATCH 06/11] Update tests for AM.Collection 6 and AM 9 - Update tests that depend on the removed feature "CreateMissingTypeMaps" (https://github.com/AutoMapper/AutoMapper/issues/3063). - Update test to not use the static Mapper.Instance and instead force to use the overload that takes the instance in the test. --- .../EntityFrameworkCoreUsingMicrosoftDITests.cs | 7 ++++--- .../EntityFramworkCoreUsingCtorTests.cs | 9 +++++---- .../EntityFramworkCoreUsingDITests.cs | 9 +++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs index db8e0ea..2f5ca3a 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs @@ -19,10 +19,11 @@ public EntityFrameworkCoreUsingMicrosoftDITests() .AddEntityFrameworkInMemoryDatabase() .AddDbContext(options => options.UseInMemoryDatabase("EfTestDatabase" + Guid.NewGuid())); - services.AddAutoMapper(automapper => + services.AddAutoMapper(x => { - automapper.AddCollectionMappers(); - automapper.UseEntityFrameworkCoreModel(services); + x.AddCollectionMappers(); + x.UseEntityFrameworkCoreModel(services); + x.CreateMap().ReverseMap(); }, new Assembly[0]); this._serviceProvider = services.BuildServiceProvider(); diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs index b0108e1..416e61b 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs @@ -6,15 +6,16 @@ namespace AutoMapper.Collection.EntityFrameworkCore.Tests { public class EntityFramworkCoreUsingCtorTests : EntityFramworkCoreTestsBase { + private readonly Mapper _mapper; + public EntityFramworkCoreUsingCtorTests() { - Mapper.Reset(); - Mapper.Initialize(x => + _mapper = new Mapper(new MapperConfiguration(x => { x.AddCollectionMappers(); x.CreateMap().ReverseMap(); x.UseEntityFrameworkCoreModel(); - }); + })); } protected override DBContextBase GetDbContext() @@ -24,7 +25,7 @@ protected override DBContextBase GetDbContext() protected override IMapper GetMapper() { - return Mapper.Instance; + return _mapper; } public class DB : DBContextBase diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs index 1ec2dc4..a9074b3 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs @@ -9,6 +9,7 @@ namespace AutoMapper.Collection.EntityFrameworkCore.Tests public class EntityFramworkCoreUsingDITests : EntityFramworkCoreTestsBase, IDisposable { private readonly ServiceProvider _serviceProvider; + private readonly Mapper _mapper; private readonly IServiceScope _serviceScope; public EntityFramworkCoreUsingDITests() @@ -21,13 +22,13 @@ public EntityFramworkCoreUsingDITests() _serviceProvider = services.BuildServiceProvider(); - Mapper.Reset(); - Mapper.Initialize(x => + _mapper = new Mapper(new MapperConfiguration(x => { x.ConstructServicesUsing(type => ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, type)); x.AddCollectionMappers(); x.UseEntityFrameworkCoreModel(_serviceProvider); - }); + x.CreateMap().ReverseMap(); + })); _serviceScope = _serviceProvider.GetRequiredService().CreateScope(); } @@ -45,7 +46,7 @@ protected override DBContextBase GetDbContext() protected override IMapper GetMapper() { - return Mapper.Instance; + return _mapper; } public class DB : DBContextBase From d92ceecbe2d2e4c1e3ff2bd732e0785cab34b017 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Tue, 13 Aug 2019 11:38:35 +0200 Subject: [PATCH 07/11] Bump version to 1.0.0 --- version.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.props b/version.props index 32de360..e7bbeca 100644 --- a/version.props +++ b/version.props @@ -1,5 +1,5 @@ - 0.3.0 + 1.0.0 From 7059445e6a622bfcb4b8a03bc3a451efe7932fe0 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Tue, 13 Aug 2019 12:16:43 +0200 Subject: [PATCH 08/11] Also run test for netcoreapp2.0 --- .../AutoMapper.Collection.EntityFrameworkCore.Tests.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj index b1f4053..0e89686 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj @@ -1,7 +1,7 @@  - net462 + net462;netcoreapp2.0 AutoMapper.Collection.EntityFrameworkCore.Tests false @@ -19,7 +19,7 @@ - + From bdcb77a40ae7f1d4e0f2fd1fa85ba9b4a62c8864 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Thu, 15 Aug 2019 12:31:31 +0200 Subject: [PATCH 09/11] Update readme --- README.md | 50 ++++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 82a6453..956abac 100644 --- a/README.md +++ b/README.md @@ -7,29 +7,20 @@ ## Configuration examples - Usage together with Dependency injection and AutoMapper.Extensions.Microsoft.DependencyInjection pacakge - ``` - var services = new ServiceCollection(); - services - .AddEntityFrameworkInMemoryDatabase() - .AddDbContext(); - - services.AddAutoMapper((serviceProvider, automapper) => - { - automapper.AddCollectionMappers(); - automapper.UseEntityFrameworkCoreModel(serviceProvider); - }, typeof(DB).Assembly); - - var serviceProvider = services.BuildServiceProvider(); - ``` - - - Simple usage with static version of AutoMapper - ``` - Mapper.Initialize(x => - { - x.AddCollectionMappers(); - x.UseEntityFrameworkCoreModel(); - }); - ``` +``` +var services = new ServiceCollection(); +services + .AddEntityFrameworkInMemoryDatabase() + .AddDbContext(); + +services.AddAutoMapper((serviceProvider, automapper) => +{ + automapper.AddCollectionMappers(); + automapper.UseEntityFrameworkCoreModel(serviceProvider); +}, typeof(DB).Assembly); + +var serviceProvider = services.BuildServiceProvider(); +``` **Note:** User defined equality expressions will overwrite primary key expressions. @@ -39,10 +30,12 @@ Automapper.Collection.EntityFrameworkCore does that as well through extension me Translate equality between dto and EF object to an expression of just the EF using the dto's values as constants. - dbContext.Orders.Persist().InsertOrUpdate(newOrderDto); - dbContext.Orders.Persist().InsertOrUpdate(existingOrderDto); - dbContext.Orders.Persist().Remove(deletedOrderDto); +``` + dbContext.Orders.Persist(mapper).InsertOrUpdate(newOrderDto); + dbContext.Orders.Persist(mapper).InsertOrUpdate(existingOrderDto); + dbContext.Orders.Persist(mapper).Remove(deletedOrderDto); dbContext.SubmitChanges(); +``` **Note:** This is done by converting the OrderDTO to Expression> and using that to find matching type in the database. You can also map objects to expressions as well. @@ -51,6 +44,7 @@ Persist doesn't call submit changes automatically How to get it -------------------------------- Use NuGet Package Manager to install the package or use any of the following command in NuGet Package Manager Console. - - PM> Install-Package AutoMapper.Collection.EntityFrameworkCore +``` +PM> Install-Package AutoMapper.Collection.EntityFrameworkCore +``` From a9f61863a158799a44c666b5f64e690bb64bf974 Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Thu, 15 Aug 2019 12:31:50 +0200 Subject: [PATCH 10/11] Update nuget.config --- NuGet.Config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NuGet.Config b/NuGet.Config index 7a2818f..4ae1f59 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -1,6 +1,6 @@  - + \ No newline at end of file From 91d2b2072a535c4d0ac02c9b12ce165cffd3143a Mon Sep 17 00:00:00 2001 From: Patric Forsgard Date: Thu, 15 Aug 2019 12:32:04 +0200 Subject: [PATCH 11/11] update reference to use ci-version of am.collection. --- .../AutoMapper.Collection.EntityFrameworkCore.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj b/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj index cbc646f..da55ba9 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj +++ b/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj @@ -15,7 +15,7 @@ - +