automapperdev - AutoMapper.Collection.EntityFramework 12.0.0

Collection updating support for EntityFramework with AutoMapper. Extends DBSet

PM> Install-Package AutoMapper.Collection.EntityFramework -Version 12.0.0 -Source https://www.myget.org/F/automapperdev/api/v3/index.json

Copy to clipboard

> nuget.exe install AutoMapper.Collection.EntityFramework -Version 12.0.0 -Source https://www.myget.org/F/automapperdev/api/v3/index.json

Copy to clipboard

> dotnet add package AutoMapper.Collection.EntityFramework --version 12.0.0 --source https://www.myget.org/F/automapperdev/api/v3/index.json

Copy to clipboard
<PackageReference Include="AutoMapper.Collection.EntityFramework" Version="12.0.0" />
Copy to clipboard
source https://www.myget.org/F/automapperdev/api/v3/index.json

nuget AutoMapper.Collection.EntityFramework  ~> 12.0.0
Copy to clipboard

> choco install AutoMapper.Collection.EntityFramework --version 12.0.0 --source https://www.myget.org/F/automapperdev/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "automapperdev" -SourceLocation "https://www.myget.org/F/automapperdev/api/v2"
Install-Module -Name "AutoMapper.Collection.EntityFramework" -RequiredVersion "12.0.0" -Repository "automapperdev" 
Copy to clipboard

Browse the sources in this package using Visual Studio or WinDbg by configuring the following symbol server URL: https://www.myget.org/F/automapperdev/api/v2/symbolpackage/


AutoMapper

AutoMapper.Collection

Adds ability to map collections to existing collections without re-creating the collection object.

Will Add/Update/Delete items from a preexisting collection object based on user defined equivalency between the collection's generic item type from the source collection and the destination collection.

NuGet

How to add to AutoMapper?

Call AddCollectionMappers when configuring

Mapper.Initialize(cfg =>
{
    cfg.AddCollectionMappers();
    // Configuration code
});

Will add new IObjectMapper objects into the master mapping list.

Adding equivalency between two classes

Adding equivalence to objects is done with EqualityComparison extended from the IMappingExpression class.

cfg.CreateMap<OrderItemDTO, OrderItem>().EqualityComparison((odto, o) => odto.ID == o.ID);

Mapping OrderDTO back to Order will compare Order items list based on if their ID's match

Mapper.Map<List<OrderDTO>,List<Order>>(orderDtos, orders);

If ID's match, then AutoMapper will map OrderDTO to Order

If OrderDTO exists and Order doesn't, then AutoMapper will add a new Order mapped from OrderDTO to the collection

If Order exists and OrderDTO doesn't, then AutoMapper will remove Order from collection

Why update collection? Just recreate it

ORMs don't like setting the collection, so you need to add and remove from preexisting one.

This automates the process by just specifying what is equal to each other.

Can it just figure out the ID equivalency for me in Entity Framework?

Automapper.Collection.EntityFramework or Automapper.Collection.EntityFrameworkCore can do that for you.

Mapper.Initialize(cfg =>
{
    cfg.AddCollectionMappers();
// entity framework
    cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>();
// entity framework core
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<DB>>();
    // Configuration code
});

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

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<OrderDTO>(newOrderDto);
dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(existingOrderDto);
dbContext.Orders.Persist().Remove<OrderDTO>(deletedOrderDto);
dbContext.SubmitChanges();

Note: This is done by converting the OrderDTO to Expression<Func<Order,bool>> and using that to find matching type in the database. You can also map objects to expressions as well.

Persist doesn't call submit changes automatically

Where can I get it?

First, install NuGet. Then, install AutoMapper.Collection from the package manager console:

PM> Install-Package AutoMapper.Collection

Additional packages

AutoMapper Collection for Entity Framework

PM> Install-Package AutoMapper.Collection.EntityFramework

AutoMapper Collection for Entity Framework Core

PM> Install-Package AutoMapper.Collection.EntityFrameworkCore

AutoMapper Collection for LinqToSQL

PM> Install-Package AutoMapper.Collection.LinqToSQL
  • .NETFramework 8.0
    • AutoMapper.Collection (>= 12.0.0)
    • AutoMapper.Extensions.ExpressionMapping (>= 9.0.1)
    • EntityFramework (>= 6.5.1)
  • .NETFramework 9.0
    • AutoMapper.Collection (>= 12.0.0)
    • AutoMapper.Extensions.ExpressionMapping (>= 9.0.1)
    • EntityFramework (>= 6.5.1)
  • .NETFramework 8.0: 8.0.0.0
  • .NETFramework 9.0: 9.0.0.0

Owners

Jimmy Bogard

Authors

Tyler Carlson

Project URL

https://github.com/AutoMapper/Automapper.Collection

License

MIT

Info

357 total downloads
1 downloads for version 12.0.0
Download (14.52 KB)
Download symbols (18.76 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
12.0.0 14.52 KB Fri, 07 Nov 2025 21:44:15 GMT 1
11.0.1-alpha.0.1 14.58 KB Fri, 07 Nov 2025 18:54:12 GMT 2
11.0.0 9.23 KB Sun, 16 Feb 2025 11:05:39 GMT 6
10.0.1-alpha.0.3 9.27 KB Sun, 16 Feb 2025 11:02:23 GMT 3
10.0.1-alpha.0.2 8.85 KB Sun, 16 Feb 2025 09:14:19 GMT 3
10.0.1-alpha.0.1 8.79 KB Thu, 11 Jul 2024 13:10:49 GMT 3
10.0.0 8.77 KB Mon, 12 Feb 2024 08:28:26 GMT 5
9.0.1-alpha.0.3 8.8 KB Mon, 12 Feb 2024 08:23:50 GMT 3
9.0.1-alpha.0.2 8.67 KB Sun, 15 Jan 2023 16:00:43 GMT 7
9.0.0 8.62 KB Wed, 05 Oct 2022 17:26:44 GMT 4
8.0.1-alpha.0.1 8.66 KB Tue, 04 Oct 2022 10:54:31 GMT 5
8.0.0 8.63 KB Thu, 31 Mar 2022 15:08:46 GMT 6
7.0.2-alpha.0.2 8.65 KB Thu, 31 Mar 2022 15:02:25 GMT 4
7.0.2-alpha.0.1 13.42 KB Fri, 12 Mar 2021 03:57:38 GMT 5
7.0.1 13.35 KB Mon, 23 Nov 2020 06:05:37 GMT 6
7.0.1-ci-00105 11.88 KB Sun, 15 Nov 2020 00:15:28 GMT 4
7.0.1-alpha.0.7 13.41 KB Mon, 23 Nov 2020 06:00:02 GMT 6
7.0.0 11.76 KB Sun, 05 Jul 2020 22:09:47 GMT 4
7.0.0-ci-00104 11.87 KB Sun, 15 Nov 2020 00:07:33 GMT 4
7.0.0-ci-00102 11.88 KB Sun, 05 Jul 2020 22:04:48 GMT 6
6.0.0 11.75 KB Wed, 25 Dec 2019 01:37:46 GMT 14
6.0.0-ci-00101 11.86 KB Sun, 05 Jul 2020 22:02:54 GMT 5
6.0.0-ci-00100 11.86 KB Sun, 05 Jul 2020 22:01:18 GMT 4
6.0.0-ci-00099 11.85 KB Wed, 25 Dec 2019 02:03:29 GMT 7
6.0.0-ci-00097 11.85 KB Wed, 25 Dec 2019 01:36:27 GMT 4
6.0.0-ci-00096 11.86 KB Tue, 22 Oct 2019 19:16:05 GMT 5
6.0.0-ci-00095 7.02 KB Thu, 15 Aug 2019 00:23:30 GMT 4
6.0.0-ci-00094 7.02 KB Thu, 15 Aug 2019 00:16:36 GMT 4
5.0.1-ci-00093 7 KB Mon, 03 Jun 2019 16:16:35 GMT 7
5.0.0 6.93 KB Tue, 20 Nov 2018 19:02:46 GMT 18
5.0.0-ci-00092 6.99 KB Mon, 06 May 2019 23:15:19 GMT 4
5.0.0-ci-00087 6.99 KB Sun, 18 Nov 2018 09:52:43 GMT 4
5.0.0-ci-00085 6.99 KB Sun, 18 Nov 2018 09:47:27 GMT 5
4.1.0-ci-00084 6.99 KB Thu, 23 Aug 2018 02:17:30 GMT 6
4.1.0-ci-00083 7 KB Thu, 26 Jul 2018 22:13:35 GMT 4
4.0.0 6.92 KB Thu, 26 Jul 2018 18:15:16 GMT 5
4.0.0-ci-00082 6.98 KB Thu, 26 Jul 2018 19:54:49 GMT 4
3.1.4 6.94 KB Wed, 27 Jun 2018 23:45:28 GMT 5
3.1.4-ci-00071 7 KB Wed, 06 Jun 2018 20:55:12 GMT 5
3.1.4-ci-00067 6.92 KB Fri, 29 Dec 2017 18:51:13 GMT 5
3.1.4-ci-00066 6.92 KB Wed, 27 Dec 2017 17:53:24 GMT 4
3.1.3 6.86 KB Mon, 27 Nov 2017 22:11:29 GMT 5
3.1.3-ci-00069 6.92 KB Tue, 15 May 2018 17:25:12 GMT 5
3.1.3-ci-00068 6.92 KB Fri, 09 Feb 2018 04:44:35 GMT 5
3.1.3-ci-00065 6.92 KB Tue, 26 Dec 2017 20:32:01 GMT 5
3.1.3-ci-00063 6.92 KB Mon, 27 Nov 2017 22:09:57 GMT 6
3.1.3-ci-00062 6.92 KB Mon, 27 Nov 2017 20:31:49 GMT 4
3.1.3-ci-00060 6.91 KB Wed, 09 Aug 2017 00:29:44 GMT 4
3.1.2 6.85 KB Tue, 08 Aug 2017 23:59:23 GMT 9
3.1.2-ci-00058 6.91 KB Tue, 08 Aug 2017 23:57:21 GMT 4
3.1.2-ci-00057 6.91 KB Wed, 12 Jul 2017 04:52:27 GMT 6
3.1.2-ci-00056 6.91 KB Fri, 07 Jul 2017 04:23:25 GMT 6
3.1.2-ci-00054 6.9 KB Fri, 07 Jul 2017 04:18:42 GMT 6
3.1.1 6.85 KB Fri, 07 Jul 2017 04:05:24 GMT 4
3.1.1-ci-00055 6.9 KB Fri, 07 Jul 2017 04:21:09 GMT 5
3.1.1-ci-00050 6.91 KB Fri, 07 Jul 2017 03:40:45 GMT 6
3.1.1-ci-00047 6.91 KB Tue, 04 Jul 2017 18:45:52 GMT 5
3.1.1-ci-00045 6.89 KB Tue, 04 Jul 2017 18:40:46 GMT 4
3.1.0-ci-00041 6.9 KB Tue, 04 Jul 2017 00:48:50 GMT 5
3.1.0-ci-00040 6.9 KB Tue, 04 Jul 2017 00:43:21 GMT 5
3.1.0-ci-00039 6.57 KB Fri, 30 Jun 2017 05:26:39 GMT 4
3.1.0-ci-00037 6.56 KB Fri, 30 Jun 2017 05:19:21 GMT 4
3.0.1 6.51 KB Thu, 25 May 2017 03:38:25 GMT 5
3.0.1-ci-00035 6.57 KB Fri, 30 Jun 2017 05:08:32 GMT 5
3.0.1-ci-00030 6.57 KB Thu, 25 May 2017 04:34:52 GMT 5
3.0.0-alpha-00013 5.46 KB Thu, 16 Mar 2017 16:23:33 GMT 7
3.0.0-alpha-00012 5.46 KB Thu, 26 Jan 2017 05:51:31 GMT 6
3.0.0-alpha-00011 5.46 KB Fri, 20 Jan 2017 07:24:52 GMT 7
3.0.0-alpha-00010 5.16 KB Fri, 20 Jan 2017 06:56:30 GMT 5