2018-07-26 18:48:14 +03:00
< img src = "https://s3.amazonaws.com/automapper/logo.png" alt = "AutoMapper" >
2018-11-25 16:51:53 +03:00
# AutoMapper.Collection.EntityFrameworkCore
2018-07-26 18:48:14 +03:00
`Automapper.Collection.EntityFrameworkCore` will help you mapping of EntityFrameowrk Core DbContext-object.
2018-11-25 16:51:53 +03:00
## Configuration examples
- Usage together with Dependency injection and AutoMapper.Extensions.Microsoft.DependencyInjection pacakge
```
var services = new ServiceCollection();
services
.AddEntityFrameworkInMemoryDatabase()
.AddDbContext< DB > ();
2019-04-27 00:45:51 +03:00
services.AddAutoMapper((serviceProvider, automapper) =>
2018-11-25 16:51:53 +03:00
{
automapper.AddCollectionMappers();
2019-04-27 00:45:51 +03:00
automapper.UseEntityFrameworkCoreModel< DB > (serviceProvider);
2018-11-25 16:51:53 +03:00
}, typeof(DB).Assembly);
var serviceProvider = services.BuildServiceProvider();
```
2019-08-13 12:51:17 +03:00
- Simple usage with static version of AutoMapper
2018-11-25 16:51:53 +03:00
```
Mapper.Initialize(x =>
{
x.AddCollectionMappers();
x.UseEntityFrameworkCoreModel< DB > ();
});
```
**Note:** User defined equality expressions will overwrite primary key expressions.
2018-07-26 18:48:14 +03:00
What about comparing to a single existing Entity for updating?
--------------------------------
2018-11-25 16:51:53 +03:00
Automapper.Collection.EntityFrameworkCore does that as well through extension method from of DbSet< TEntity > .
2018-07-26 18:48:14 +03:00
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
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
2018-08-06 19:34:04 +03:00