Skip to content

Commit 251df9e

Browse files
Merge pull request #24 from AutoMapper/development
merge to master for release
2 parents 0574956 + 6d8d281 commit 251df9e

12 files changed

+60
-46
lines changed

NuGet.Config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3-
<packageSources>
3+
<packageSources>
44
<add key="AutoMapper" value="https://www.myget.org/F/automapperdev/api/v2" />
55
</packageSources>
66
</configuration>

README.md

+32-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
<img src="https://s3.amazonaws.com/automapper/logo.png" alt="AutoMapper">
22

3-
AutoMapper.Collection.EFCore
4-
================================
5-
`Automapper.Collection.EntityFrameworkCore` will help you mapping of EntityFramework Core DbContext-object.
6-
7-
Mapper.Initialize(cfg =>
8-
{
9-
cfg.AddCollectionMappers();
10-
cfg.UseEntityFrameworkCoreModel<DB>(serviceProvider);
11-
// Configuration code
12-
});
13-
14-
User defined equality expressions will overwrite primary key expressions.
3+
# AutoMapper.Collection.EntityFrameworkCore
4+
5+
`Automapper.Collection.EntityFrameworkCore` will help you mapping of EntityFrameowrk Core DbContext-object.
6+
7+
## Configuration examples
8+
9+
- Usage together with Dependency injection and AutoMapper.Extensions.Microsoft.DependencyInjection pacakge
10+
```
11+
var services = new ServiceCollection();
12+
services
13+
.AddEntityFrameworkInMemoryDatabase()
14+
.AddDbContext<DB>();
15+
16+
services.AddAutoMapper((serviceProvider, automapper) =>
17+
{
18+
automapper.AddCollectionMappers();
19+
automapper.UseEntityFrameworkCoreModel<DB>(serviceProvider);
20+
}, typeof(DB).Assembly);
21+
22+
var serviceProvider = services.BuildServiceProvider();
23+
```
24+
25+
**Note:** User defined equality expressions will overwrite primary key expressions.
1526

1627
What about comparing to a single existing Entity for updating?
1728
--------------------------------
18-
Automapper.Collection.EntityFramework does that as well through extension method from of DbSet<TEntity>.
29+
Automapper.Collection.EntityFrameworkCore does that as well through extension method from of DbSet<TEntity>.
1930

2031
Translate equality between dto and EF object to an expression of just the EF using the dto's values as constants.
2132

22-
dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(newOrderDto);
23-
dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(existingOrderDto);
24-
dbContext.Orders.Persist().Remove<OrderDTO>(deletedOrderDto);
33+
```
34+
dbContext.Orders.Persist(mapper).InsertOrUpdate<OrderDTO>(newOrderDto);
35+
dbContext.Orders.Persist(mapper).InsertOrUpdate<OrderDTO>(existingOrderDto);
36+
dbContext.Orders.Persist(mapper).Remove<OrderDTO>(deletedOrderDto);
2537
dbContext.SubmitChanges();
38+
```
2639

2740
**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.
2841

@@ -31,6 +44,7 @@ Persist doesn't call submit changes automatically
3144
How to get it
3245
--------------------------------
3346
Use NuGet Package Manager to install the package or use any of the following command in NuGet Package Manager Console.
34-
35-
PM> Install-Package AutoMapper.Collection.EntityFrameworkCore
3647

48+
```
49+
PM> Install-Package AutoMapper.Collection.EntityFrameworkCore
50+
```

build.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ task compile -depends clean {
4949
# restore all project references (creating project.assets.json for each project)
5050
exec { dotnet restore $base_dir\AutoMapper.Collection.EFCore.sln /nologo }
5151

52-
exec { dotnet build $base_dir\AutoMapper.Collection.EFCore.sln -c $config $buildParam /nologo --no-restore }
52+
exec { dotnet build $base_dir\AutoMapper.Collection.EFCore.sln -c $config $buildParam --no-restore /nologo }
5353

5454
exec { dotnet pack $base_dir\AutoMapper.Collection.EFCore.sln -c $config --include-symbols --no-build --no-restore --output $artifacts_dir $packageParam /nologo}
5555

src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net462</TargetFrameworks>
4+
<TargetFrameworks>net462;netcoreapp2.0</TargetFrameworks>
55
<AssemblyName>AutoMapper.Collection.EntityFrameworkCore.Tests</AssemblyName>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
@@ -19,7 +19,7 @@
1919
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.0.0" />
2020
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.1" />
2121
<PackageReference Include="FluentAssertions" Version="5.4.1" />
22-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
22+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
2323
<PackageReference Include="xunit" Version="2.4.1" />
2424
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
2525
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />

src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ public EntityFrameworkCoreUsingMicrosoftDITests()
1919
.AddEntityFrameworkInMemoryDatabase()
2020
.AddDbContext<DB>(options => options.UseInMemoryDatabase("EfTestDatabase" + Guid.NewGuid()));
2121

22-
services.AddAutoMapper(automapper =>
22+
services.AddAutoMapper(x =>
2323
{
24-
automapper.AddCollectionMappers();
25-
automapper.UseEntityFrameworkCoreModel<DB>(services);
24+
x.AddCollectionMappers();
25+
x.UseEntityFrameworkCoreModel<DB>(services);
26+
x.CreateMap<ThingDto, Thing>().ReverseMap();
2627
}, new Assembly[0]);
2728

2829
this._serviceProvider = services.BuildServiceProvider();

src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ namespace AutoMapper.Collection.EntityFrameworkCore.Tests
66
{
77
public class EntityFramworkCoreUsingCtorTests : EntityFramworkCoreTestsBase
88
{
9+
private readonly Mapper _mapper;
10+
911
public EntityFramworkCoreUsingCtorTests()
1012
{
11-
Mapper.Reset();
12-
Mapper.Initialize(x =>
13+
_mapper = new Mapper(new MapperConfiguration(x =>
1314
{
1415
x.AddCollectionMappers();
1516
x.CreateMap<ThingDto, Thing>().ReverseMap();
1617
x.UseEntityFrameworkCoreModel<DB>();
17-
});
18+
}));
1819
}
1920

2021
protected override DBContextBase GetDbContext()
@@ -24,7 +25,7 @@ protected override DBContextBase GetDbContext()
2425

2526
protected override IMapper GetMapper()
2627
{
27-
return Mapper.Instance;
28+
return _mapper;
2829
}
2930

3031
public class DB : DBContextBase

src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace AutoMapper.Collection.EntityFrameworkCore.Tests
99
public class EntityFramworkCoreUsingDITests : EntityFramworkCoreTestsBase, IDisposable
1010
{
1111
private readonly ServiceProvider _serviceProvider;
12+
private readonly Mapper _mapper;
1213
private readonly IServiceScope _serviceScope;
1314

1415
public EntityFramworkCoreUsingDITests()
@@ -21,13 +22,13 @@ public EntityFramworkCoreUsingDITests()
2122

2223
_serviceProvider = services.BuildServiceProvider();
2324

24-
Mapper.Reset();
25-
Mapper.Initialize(x =>
25+
_mapper = new Mapper(new MapperConfiguration(x =>
2626
{
2727
x.ConstructServicesUsing(type => ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, type));
2828
x.AddCollectionMappers();
2929
x.UseEntityFrameworkCoreModel<DB>(_serviceProvider);
30-
});
30+
x.CreateMap<ThingDto, Thing>().ReverseMap();
31+
}));
3132

3233
_serviceScope = _serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
3334
}
@@ -45,7 +46,7 @@ protected override DBContextBase GetDbContext()
4546

4647
protected override IMapper GetMapper()
4748
{
48-
return Mapper.Instance;
49+
return _mapper;
4950
}
5051

5152
public class DB : DBContextBase

src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="AutoMapper.Collection" Version="5.0.0" />
18+
<PackageReference Include="AutoMapper.Collection" Version="6.0.0-ci-*" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ namespace AutoMapper.EntityFrameworkCore
99
public static class Extensions
1010
{
1111
/// <summary>
12+
/// Obsolete: Use Persist(IMapper) instead.
1213
/// Create a Persistence object for the <see cref="T:System.Data.Entity.DbSet`1"/> to have data persisted or removed from
1314
/// Uses static API's Mapper for finding TypeMap between classes
1415
/// </summary>
1516
/// <typeparam name="TSource">Source table type to be updated</typeparam>
1617
/// <param name="source">DbSet to be updated</param>
1718
/// <returns>Persistence object to Update or Remove data</returns>
19+
[Obsolete("Use Persist(IMapper) instead.", true)]
1820
public static IPersistence<TSource> Persist<TSource>(this DbSet<TSource> source)
1921
where TSource : class
2022
{
21-
return new Persistence<TSource>(source, Mapper.Instance);
23+
throw new NotSupportedException();
2224
}
2325

2426
/// <summary>

src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<TDatabaseContext>
1414

1515
public GenerateEntityFrameworkCorePrimaryKeyPropertyMaps()
1616
{
17-
throw new InvalidOperationException("Use UseEntityFrameworkCoreModel instead of using SetGeneratePropertyMaps.");
17+
throw new InvalidOperationException($"Use {nameof(MapperConfigurationExpressionExtensions.UseEntityFrameworkCoreModel)} instead of using SetGeneratePropertyMaps.");
1818
}
1919

2020
public GenerateEntityFrameworkCorePrimaryKeyPropertyMaps(IModel model) => _model = model;

src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs

+4-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Persistence<TTo> : IPersistence<TTo>
1616
public Persistence(DbSet<TTo> sourceSet, IMapper mapper)
1717
{
1818
_sourceSet = sourceSet;
19-
_mapper = mapper;
19+
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
2020
}
2121

2222
public TTo InsertOrUpdate<TFrom>(TFrom from)
@@ -85,15 +85,12 @@ private TTo MapObject(Type type, object from, TTo to)
8585
{
8686
if (to == null)
8787
{
88-
to = (TTo)(_mapper?.Map(from, type, typeof(TTo)) ?? Mapper.Map(from, type, typeof(TTo)));
88+
to = (TTo)_mapper.Map(from, type, typeof(TTo));
8989
_sourceSet.Add(to);
9090
}
9191
else
9292
{
93-
if (_mapper == null)
94-
Mapper.Map(from, to);
95-
else
96-
_mapper.Map(from, to);
93+
_mapper.Map(from, to);
9794
}
9895
return to;
9996
}
@@ -105,9 +102,7 @@ private Expression<Func<TTo, bool>> GetEquivalenceExpression<TFrom>(TFrom from)
105102

106103
private Expression<Func<TTo, bool>> GetEquivalenceExpression(Type type, object from)
107104
{
108-
return _mapper == null
109-
? Mapper.Map(from, type, typeof(Expression<Func<TTo, bool>>)) as Expression<Func<TTo, bool>>
110-
: _mapper.Map(from, type, typeof(Expression<Func<TTo, bool>>)) as Expression<Func<TTo, bool>>;
105+
return _mapper.Map(from, type, typeof(Expression<Func<TTo, bool>>)) as Expression<Func<TTo, bool>>;
111106
}
112107
}
113108
}

version.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>0.3.0</VersionPrefix>
3+
<VersionPrefix>1.0.0</VersionPrefix>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)