diff --git a/NuGet.Config b/NuGet.Config index 7a2818f..4ae1f59 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -1,6 +1,6 @@  - + \ No newline at end of file diff --git a/README.md b/README.md index 73414b8..956abac 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,41 @@ AutoMapper -AutoMapper.Collection.EFCore -================================ -`Automapper.Collection.EntityFrameworkCore` will help you mapping of EntityFramework Core DbContext-object. - - Mapper.Initialize(cfg => - { - cfg.AddCollectionMappers(); - cfg.UseEntityFrameworkCoreModel(serviceProvider); - // Configuration code - }); - -User defined equality expressions will overwrite primary key expressions. +# AutoMapper.Collection.EntityFrameworkCore + +`Automapper.Collection.EntityFrameworkCore` will help you mapping of EntityFrameowrk Core DbContext-object. + +## Configuration examples + +- Usage together with Dependency injection and AutoMapper.Extensions.Microsoft.DependencyInjection pacakge +``` +var services = new ServiceCollection(); +services + .AddEntityFrameworkInMemoryDatabase() + .AddDbContext(); + +services.AddAutoMapper((serviceProvider, automapper) => +{ + automapper.AddCollectionMappers(); + automapper.UseEntityFrameworkCoreModel(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.EntityFramework does that as well through extension method from of DbSet. +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().InsertOrUpdate(newOrderDto); - dbContext.Orders.Persist().InsertOrUpdate(existingOrderDto); - dbContext.Orders.Persist().Remove(deletedOrderDto); +``` + dbContext.Orders.Persist(mapper).InsertOrUpdate(newOrderDto); + dbContext.Orders.Persist(mapper).InsertOrUpdate(existingOrderDto); + dbContext.Orders.Persist(mapper).Remove(deletedOrderDto); dbContext.SubmitChanges(); +``` **Note:** This is done by converting the OrderDTO to Expression> and using that to find matching type in the database. You can also map objects to expressions as well. @@ -31,6 +44,7 @@ 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 +``` +PM> Install-Package AutoMapper.Collection.EntityFrameworkCore +``` diff --git a/build.ps1 b/build.ps1 index 468a580..2d198f4 100644 --- a/build.ps1 +++ b/build.ps1 @@ -49,7 +49,7 @@ task compile -depends clean { # restore all project references (creating project.assets.json for each project) exec { dotnet restore $base_dir\AutoMapper.Collection.EFCore.sln /nologo } - exec { dotnet build $base_dir\AutoMapper.Collection.EFCore.sln -c $config $buildParam /nologo --no-restore } + exec { dotnet build $base_dir\AutoMapper.Collection.EFCore.sln -c $config $buildParam --no-restore /nologo } exec { dotnet pack $base_dir\AutoMapper.Collection.EFCore.sln -c $config --include-symbols --no-build --no-restore --output $artifacts_dir $packageParam /nologo} diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj index b1f4053..0e89686 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/AutoMapper.Collection.EntityFrameworkCore.Tests.csproj @@ -1,7 +1,7 @@  - net462 + net462;netcoreapp2.0 AutoMapper.Collection.EntityFrameworkCore.Tests false @@ -19,7 +19,7 @@ - + diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs index db8e0ea..2f5ca3a 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFrameworkCoreUsingMicrosoftDITests.cs @@ -19,10 +19,11 @@ public EntityFrameworkCoreUsingMicrosoftDITests() .AddEntityFrameworkInMemoryDatabase() .AddDbContext(options => options.UseInMemoryDatabase("EfTestDatabase" + Guid.NewGuid())); - services.AddAutoMapper(automapper => + services.AddAutoMapper(x => { - automapper.AddCollectionMappers(); - automapper.UseEntityFrameworkCoreModel(services); + x.AddCollectionMappers(); + x.UseEntityFrameworkCoreModel(services); + x.CreateMap().ReverseMap(); }, new Assembly[0]); this._serviceProvider = services.BuildServiceProvider(); diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs index b0108e1..416e61b 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingCtorTests.cs @@ -6,15 +6,16 @@ namespace AutoMapper.Collection.EntityFrameworkCore.Tests { public class EntityFramworkCoreUsingCtorTests : EntityFramworkCoreTestsBase { + private readonly Mapper _mapper; + public EntityFramworkCoreUsingCtorTests() { - Mapper.Reset(); - Mapper.Initialize(x => + _mapper = new Mapper(new MapperConfiguration(x => { x.AddCollectionMappers(); x.CreateMap().ReverseMap(); x.UseEntityFrameworkCoreModel(); - }); + })); } protected override DBContextBase GetDbContext() @@ -24,7 +25,7 @@ protected override DBContextBase GetDbContext() protected override IMapper GetMapper() { - return Mapper.Instance; + return _mapper; } public class DB : DBContextBase diff --git a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs index 1ec2dc4..a9074b3 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore.Tests/EntityFramworkCoreUsingDITests.cs @@ -9,6 +9,7 @@ namespace AutoMapper.Collection.EntityFrameworkCore.Tests public class EntityFramworkCoreUsingDITests : EntityFramworkCoreTestsBase, IDisposable { private readonly ServiceProvider _serviceProvider; + private readonly Mapper _mapper; private readonly IServiceScope _serviceScope; public EntityFramworkCoreUsingDITests() @@ -21,13 +22,13 @@ public EntityFramworkCoreUsingDITests() _serviceProvider = services.BuildServiceProvider(); - Mapper.Reset(); - Mapper.Initialize(x => + _mapper = new Mapper(new MapperConfiguration(x => { x.ConstructServicesUsing(type => ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, type)); x.AddCollectionMappers(); x.UseEntityFrameworkCoreModel(_serviceProvider); - }); + x.CreateMap().ReverseMap(); + })); _serviceScope = _serviceProvider.GetRequiredService().CreateScope(); } @@ -45,7 +46,7 @@ protected override DBContextBase GetDbContext() protected override IMapper GetMapper() { - return Mapper.Instance; + return _mapper; } public class DB : DBContextBase diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj b/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj index afc7f8d..da55ba9 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj +++ b/src/AutoMapper.Collection.EntityFrameworkCore/AutoMapper.Collection.EntityFrameworkCore.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs b/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs index bc50cb3..ad4e004 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore/Extensions.cs @@ -9,16 +9,18 @@ namespace AutoMapper.EntityFrameworkCore public static class Extensions { /// + /// Obsolete: Use Persist(IMapper) instead. /// Create a Persistence object for the to have data persisted or removed from /// Uses static API's Mapper for finding TypeMap between classes /// /// Source table type to be updated /// DbSet to be updated /// Persistence object to Update or Remove data + [Obsolete("Use Persist(IMapper) instead.", true)] public static IPersistence Persist(this DbSet source) where TSource : class { - return new Persistence(source, Mapper.Instance); + throw new NotSupportedException(); } /// diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs b/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs index aefe9b7..584f4d0 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore/GenerateEntityFrameworkCorePrimaryKeyPropertyMaps.cs @@ -14,7 +14,7 @@ public class GenerateEntityFrameworkCorePrimaryKeyPropertyMaps public GenerateEntityFrameworkCorePrimaryKeyPropertyMaps() { - throw new InvalidOperationException("Use UseEntityFrameworkCoreModel instead of using SetGeneratePropertyMaps."); + throw new InvalidOperationException($"Use {nameof(MapperConfigurationExpressionExtensions.UseEntityFrameworkCoreModel)} instead of using SetGeneratePropertyMaps."); } public GenerateEntityFrameworkCorePrimaryKeyPropertyMaps(IModel model) => _model = model; diff --git a/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs b/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs index afd77a9..903e78c 100644 --- a/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs +++ b/src/AutoMapper.Collection.EntityFrameworkCore/Persistence.cs @@ -16,7 +16,7 @@ public class Persistence : IPersistence public Persistence(DbSet sourceSet, IMapper mapper) { _sourceSet = sourceSet; - _mapper = mapper; + _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); } public TTo InsertOrUpdate(TFrom from) @@ -85,15 +85,12 @@ private TTo MapObject(Type type, object from, TTo to) { if (to == null) { - to = (TTo)(_mapper?.Map(from, type, typeof(TTo)) ?? Mapper.Map(from, type, typeof(TTo))); + to = (TTo)_mapper.Map(from, type, typeof(TTo)); _sourceSet.Add(to); } else { - if (_mapper == null) - Mapper.Map(from, to); - else - _mapper.Map(from, to); + _mapper.Map(from, to); } return to; } @@ -105,9 +102,7 @@ private Expression> GetEquivalenceExpression(TFrom from) private Expression> GetEquivalenceExpression(Type type, object from) { - return _mapper == null - ? Mapper.Map(from, type, typeof(Expression>)) as Expression> - : _mapper.Map(from, type, typeof(Expression>)) as Expression>; + return _mapper.Map(from, type, typeof(Expression>)) as Expression>; } } } \ No newline at end of file diff --git a/version.props b/version.props index 32de360..e7bbeca 100644 --- a/version.props +++ b/version.props @@ -1,5 +1,5 @@ - 0.3.0 + 1.0.0