1
0
Форкнуть 0
EFCore support for AutoMapper.Collections
Перейти к файлу
Lucian Bargaoanu 1f5aa340d6
Merge pull request #78 from slubowsky/master
Update to AutoMapper 13
2024-02-15 16:35:47 +02:00
.github/workflows Updating dependencies 2022-04-01 08:21:29 -05:00
src tab to spaces 2024-02-15 09:10:53 -05:00
.editorconfig rebase didn't work (file rename to diff), so manually re-edited my changes so hope appveyor and Collaborators happy with revised PR ! 2020-09-05 08:17:13 +02:00
.gitattributes Add init structure of files to get build/psake etc to work 2018-07-26 17:48:14 +02:00
.gitignore Add init structure of files to get build/psake etc to work 2018-07-26 17:48:14 +02:00
AutoMapper.Collection.EFCore.sln Support GitHub Actions and remove AppVouyer 2020-11-23 02:04:33 -06:00
LICENSE.txt Rename license file 2018-07-26 17:42:10 +02:00
NuGet.Config Update nuget.config 2019-08-15 12:31:50 +02:00
Push.ps1 Support GitHub Actions and remove AppVouyer 2020-11-23 02:04:33 -06:00
README.md Update README.md 2020-12-22 12:12:50 -05:00
build.ps1 Support GitHub Actions and remove AppVouyer 2020-11-23 02:04:33 -06:00
icon.png Support GitHub Actions and remove AppVouyer 2020-11-23 02:04:33 -06:00

README.md

AutoMapper

AutoMapper.Collection.EntityFrameworkCore

Automapper.Collection.EntityFrameworkCore will help you when mapping an EntityFramework Core DbContext-object.

NuGet

Configuration examples

  • Usage together with Dependency injection and AutoMapper.Extensions.Microsoft.DependencyInjection package
var services = new ServiceCollection();
services
    .AddEntityFrameworkInMemoryDatabase()
    .AddDbContext<DB>();

services.AddAutoMapper((serviceProvider, automapper) =>
{
    automapper.AddCollectionMappers();
    automapper.UseEntityFrameworkCoreModel<DB>(serviceProvider);
}, typeof(DB).Assembly);

var serviceProvider = services.BuildServiceProvider();

Note: User defined equality expressions will overwrite primary key expressions.

What about comparing to a single existing Entity for updating?

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.

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

How to get it

Use NuGet Package Manager to install the package or use any of the following commands in NuGet Package Manager Console.

PM> Install-Package AutoMapper.Collection.EntityFrameworkCore