1
0
Форкнуть 0
AutoMapper.Collection.EFCore/README.md

53 строки
2.0 KiB
Markdown
Исходник Постоянная ссылка Обычный вид История

<img src="https://s3.amazonaws.com/automapper/logo.png" alt="AutoMapper">
2018-11-25 16:51:53 +03:00
# AutoMapper.Collection.EntityFrameworkCore
`Automapper.Collection.EntityFrameworkCore` will help you when mapping an EntityFramework Core DbContext-object.
2020-03-26 07:13:09 +03:00
[![NuGet](http://img.shields.io/nuget/v/Automapper.Collection.EntityFrameworkCore.svg)](https://www.nuget.org/packages/Automapper.Collection.EntityFrameworkCore/)
2018-11-25 16:51:53 +03:00
## Configuration examples
- Usage together with Dependency injection and AutoMapper.Extensions.Microsoft.DependencyInjection package
2019-08-15 13:31:31 +03:00
```
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();
```
2018-11-25 16:51:53 +03:00
**Note:** User defined equality expressions will overwrite primary key expressions.
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>.
Translate equality between dto and EF object to an expression of just the EF using the dto's values as constants.
2019-08-15 13:31:31 +03:00
```
dbContext.Orders.Persist(mapper).InsertOrUpdate<OrderDTO>(newOrderDto);
dbContext.Orders.Persist(mapper).InsertOrUpdate<OrderDTO>(existingOrderDto);
dbContext.Orders.Persist(mapper).Remove<OrderDTO>(deletedOrderDto);
dbContext.SubmitChanges();
2019-08-15 13:31:31 +03:00
```
**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.
2018-08-06 19:34:04 +03:00
2019-08-15 13:31:31 +03:00
```
PM> Install-Package AutoMapper.Collection.EntityFrameworkCore
```