Please take a look at this rather contrived example of what I'm trying to do.
First, the database models:
public class Report
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Worker> Workers { get; set; }
}
public abstract class Worker
{
public Guid Id { get; set; }
}
public class Fireman : Worker
{
public string Station { get; set; }
}
public class Cleaner : Worker
{
public string FavoriteSolvent { get; set; }
}
Now the view models:
public class AddReportViewModel
{
public string Name { get; set; }
public List<AddFiremanViewModel> Firemen { get; set; }
public List<AddCleanerViewModel> Cleaners { get; set; }
}
public class AddFiremanViewModel
{
public string Station { get; set; }
}
public class AddCleanerViewModel
{
public string FavoriteSolvent { get; set; }
}
And finally the Automapper profile:
public class ReportProfile : Profile
{
protected override void Configure()
{
CreateMap<AddReportViewModel, Report>();
CreateMap<AddFiremanViewModel, Fireman>();
CreateMap<AddCleanerViewModel, Cleaner>();
}
}
I want the Firemen and Cleaners to both populate the Workers collection, which is an ICollection<Worker>. I hope this makes sense. How can I do this?
Your view model doesn't contain any Id field so I set those up as Ignore in the mapping. Then, I just used LINQ's Union clause to combine the two source lists into a single collection (after converting each one using Automapper). Here's the mapping:
Mapper.CreateMap<AddReportViewModel, Report>()
.ForMember(d => d.Id, o => o.Ignore())
.ForMember(d => d.Workers, o => o.MapFrom(
s => Mapper.Map<ICollection<AddFiremanViewModel>, ICollection<Fireman>>(s.Firemen)
.Union<Worker>(Mapper.Map<ICollection<AddCleanerViewModel>, ICollection<Cleaner>>(s.Cleaners))))
;
Mapper.CreateMap<AddFiremanViewModel, Fireman>()
.ForMember(d => d.Id, o => o.Ignore())
;
Mapper.CreateMap<AddCleanerViewModel, Cleaner>()
.ForMember(d => d.Id, o => o.Ignore())
;
And here's an example of using it:
var vm = new AddReportViewModel
{
Name = "Sample",
Cleaners = new List<AddCleanerViewModel>
{
new AddCleanerViewModel {FavoriteSolvent = "Alcohol"}
},
Firemen = new List<AddFiremanViewModel>
{
new AddFiremanViewModel {Station = "51"},
new AddFiremanViewModel {Station = "49"}
}
};
var db = Mapper.Map<AddReportViewModel, Report>(vm);
Related
I need to know if its possible to use Automapper to map a DTO property from a database view. Or maybe it is possible in the DbContext model configuration. Lets imagine that I have a business purpose for having a view that contains other pertinent data, but for the purposes of brevity, I have made the classes simple
Pertinent Nugets
EF Core v3.1.7
AutoMapper v10.0.0
I have an Entity
public class Foo {
public int Id { get; set; }
public string Name { get; set; }
}
I have a keyless View
public class BarVW {
public int FooId { get; set; }
public string FooName { get; set; }
}
The view is built in the DB Initializer class
context.Database.ExecuteSqlRaw(
#"CREATE OR REPLACE VIEW v_Bar AS
select
f.Id as FooId,
f.[Name] as FooName
from
Foo f
-- where some logic that makes a Foo appear in view"
);
Then the view is assigned to a DbSet in the DbContext class
modelBuilder.Entity<BarVW>(eb =>
{
eb.HasNoKey();
eb.ToView("v_Bar");
});
public DbSet<BarVW> BarVW { get; set; }
In my FooDto, I need an additional property to my entity class that will indicate that this Foo exists in my database view v_Bar
public class FooDto {
public int Id { get; set; }
public string Name { get; set; }
public bool IsBar { get; set; }
}
For the Automapper config I have the following, but there isn't an a way that i know of that i can map my dto IsBar property from the BarVW DbSet
CreateMap<Foo, FooDto>()
.ForMember(dto => dto.IsBar, opt => ??? ); // don't know what to put here
A simple way to accomplish what you want is to introduce a navigation property (e.g. Bars) and use that in the mapping configuration (e.g. opt.MapFrom(src => src.Bars.Any())).
Here is a fully working sample console program, that demonstrates this approach:
using System.Diagnostics;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace IssueConsoleTemplate
{
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public BarVW Bar { get; set; }
}
public class BarVW
{
public int FooId { get; set; }
public string FooName { get; set; }
public Foo Foo { get; set; }
}
public class Context : DbContext
{
public virtual DbSet<Foo> Foo { get; set; }
public virtual DbSet<BarVW> BarVW { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
#"Data Source=.\MSSQL14;Integrated Security=SSPI;Initial Catalog=So63850736")
.UseLoggerFactory(LoggerFactory.Create(b => b
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>()
.HasData(
new Foo {Id = 1, Name = "Fo"},
new Foo {Id = 2, Name = "Foo"},
new Foo {Id = 3, Name = "Fooo"});
modelBuilder.Entity<BarVW>(
eb =>
{
eb.HasKey(e => e.FooId);
eb.ToView("v_Bar");
eb.HasOne(e => e.Foo)
.WithOne(e => e.Bar)
.HasForeignKey<BarVW>(e => e.FooId);
});
}
}
public class FooDto
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsBar { get; set; }
}
internal static class Program
{
private static void Main()
{
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Database.ExecuteSqlRaw(
#"CREATE VIEW [v_Bar] AS
select
f.[Id] as [FooId],
f.[Name] as [FooName]
from
[Foo] f
where
f.[Id] >= 1 and f.[Id] <= 2"
);
var config = new MapperConfiguration(
cfg => cfg
.CreateMap<Foo, FooDto>()
.ForMember(dto => dto.IsBar, opt => opt.MapFrom(src => src.Bar != null)));
var result = context.Foo
.ProjectTo<FooDto>(config)
.ToList();
Debug.Assert(result.Count == 3);
Debug.Assert(result.Count(dto => dto.IsBar) == 2);
}
}
}
The generated SQL for the query looks like this:
SELECT [f].[Id], CASE
WHEN [v].[FooId] IS NOT NULL THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END AS [IsBar], [f].[Name]
FROM [Foo] AS [f]
LEFT JOIN [v_Bar] AS [v] ON [f].[Id] = [v].[FooId]
You can run the sample code using .NET Fiddle.
I'm pretty new using AutoMapper and i run into an issue
I have a model like this.
public class StepUp {
public string Example {get;set;}
public string Example2 {get;set;}
public decimal? auxValue { get;set; }
}
But i have two ViewModels as destination
public class NuevoStepUpViewModel()
{
public bool TieneAuxiliar { get; set; }
public string Example { get;set; }
public CargaDatosElectricos CargaDatosElectricos { get; set; }
}
public class CargaDatosElectricos {
public CargaDatosElectricos(bool tieneAuxiliar)
{
TieneAuxiliar = tieneAuxiliar;
}
public readonly bool TieneAuxiliar;
public string Example2 { get; set; }
}
I think some like this:
CreateMap<StepUp,NuevoStepUpViewModel()
.ForMember(x => x.TieneAuxiliar, x => x.MapFrom(c => c.auxValue.HasValue))
.ForMember(x => x.Example, x => x.MapFrom(c => c.Example))
.ForMember(x => x.CargaDatosElectricos.Example2, x => x.MapFrom(c => c.Example2))
.BeforeMap((x,y) => {
x.CargaDatosElectricos = new CargaDatosElectricos(c.auxValue.HasValue);
});
But i'm getting
Expression 'x => x.CargaDatosElectricos.Example2' must resolve to
top-level member and not any child object's properties
How should i create my mapper configuration to do this type of mapping?
There are some errors on your code. You could configure better your mapping using the AfterMap scope instead of BeforeMap to provide a complex configuration. (I am not sure but I think the) AutoMapper will not instance a property where the type is a class. So, you have to do it on the construtor of the destination class (VIewModel) or do it on AfterMap.
The TieneAuxiliar property will not allow you to set a value when it is readonly, so, you will not able to configure a map to this property. I change it to a public classic property.
See the working sample here:
https://dotnetfiddle.net/HSyUVv
using System;
using AutoMapper;
public class Program
{
public static void Main()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<StepUp, NuevoStepUpViewModel>()
.ForMember(vm => vm.TieneAuxiliar, opt => opt.MapFrom(e => e.auxValue.HasValue))
.ForMember(vm => vm.Example, opt => opt.MapFrom(e => e.Example))
.AfterMap((e, vm) =>
{
vm.CargaDatosElectricos.Example2 = e.Example2;
});
});
var mapper = config.CreateMapper();
var stepUp = new StepUp()
{
Example = "Example 1",
Example2 = "Example 2",
auxValue = 10m
};
var viewModel = mapper.Map<StepUp, NuevoStepUpViewModel>(stepUp);
Console.WriteLine("SteUp was converted to ViewModel");
Console.WriteLine("TieneAuxiliar: {0}", viewModel.TieneAuxiliar);
Console.WriteLine("Example: {0}", viewModel.Example);
Console.WriteLine("CargaDatosElectricos.TieneAuxiliar: {0}", viewModel.CargaDatosElectricos.TieneAuxiliar);
Console.WriteLine("CargaDatosElectricos.Exemple2: {0}", viewModel.CargaDatosElectricos.Example2);
}
public class StepUp
{
public string Example { get; set; }
public string Example2 { get; set; }
public decimal? auxValue { get; set; }
}
public class NuevoStepUpViewModel
{
public bool TieneAuxiliar { get; set; }
public string Example { get;set; }
public CargaDatosElectricos CargaDatosElectricos { get; set; }
public NuevoStepUpViewModel()
{
this.CargaDatosElectricos = new CargaDatosElectricos();
}
}
public class CargaDatosElectricos
{
public CargaDatosElectricos()
{
}
public bool TieneAuxiliar { get; set; }
public string Example2 { get; set; }
}
}
I have a parent class:
public class Parent
{
...
public List<Location> Locations { get; set; }
}
Location class:
public class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
}
Destination class for mapping:
public class Destination
{
...
public string DelimitedLocations { get; set; }
}
I need to map the LocationId list from Locations to a comma delimited string using automapper.
Here are several things I have tried:
CreateMap<Parent, Destination>().ForMember(d => d.DelimitedLocations , o => o.MapFrom(s => string.Join(",", s.Locations.ToList().Select(t => t.LocationID.ToString()))))
Result:
LINQ to Entities does not recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' method, and this method cannot be translated into a store expression.
Next attempt:
CreateMap<Parent, Destination>()..ForMember(d => d.TestPlotLocationsSelected, o => o.MapFrom(s => s.TestPlotLocations.ToList().Select(t => string.Join(",", t.TestPlotLocationID.ToString()))))
Result:
No method 'ToString' exists on type 'System.Collections.Generic.IEnumerable`1[System.String]'.
Not sure what to try next.
Select statement should be something like
o.Locations.Select(x => x.LocationId).ToList()
Demo
public class Program
{
public static void Main()
{
Initialize();
var source = new Parent
{
Locations = new List<Location>
{
new Location {LocationId = 1, Name = "One"},
new Location {LocationId = 2, Name = "Two"},
new Location {LocationId = 3, Name = "Three"},
}
};
var destination = Mapper.Map<Parent, Destination>(source);
Console.ReadLine();
}
public static void Initialize()
{
MapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Parent, Destination>()
.ForMember(dest => dest.DelimitedLocations, mo => mo.MapFrom(src =>
src.Locations != null
? string.Join(",", src.Locations.Select(x => x.LocationId).ToList())
: ""));
});
Mapper = MapperConfiguration.CreateMapper();
}
public static IMapper Mapper { get; private set; }
public static MapperConfiguration MapperConfiguration { get; private set; }
}
public class Parent
{
public List<Location> Locations { get; set; }
}
public class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
}
public class Destination
{
public string DelimitedLocations { get; set; }
}
Result
I'm new to Automapper and am trying to map the data from an object in a list to an interface in a list. Add to that the lists are in different levels of the containing objects:
// class definitions
public class MyViewModel
{
public int ViewModelId { get; set; }
public IList<ViewItem> Page1Selections { get; set; }
public IList<ViewItem> Page2Selections { get; set; }
}
public class ViewItem
{
public int ItemId { get; set; }
public bool IsSelected { get; set; }
}
public class MyDbModel
{
public int DbModelId { get; set; }
public IPageSelection Page1Selections { get; set; }
public IPageSelection Page2Selections { get; set; }
}
public interface IPageSelection
{
int PageNumber { get; set; }
IList<IMyDbItem> PageSelections { get; set; }
}
public class PageSelection : IPageSelection
{
public int PageNumber { get; set; }
public IList<IMyDbItem> PageSelections { get; set; }
}
public interface IMyDbItem
{
int ItemId { get; set; }
bool IsSelected { get; set; }
}
public class MyDbItem : IMyDbItem
{
public int ItemId { get; set; }
public bool IsSelected { get; set; }
}
// mapping code
MyViewModel myVm = new MyViewModel();
myVm.ViewModelId = 123;
myVm.Page1Selections = new List<ViewItem>();
myVm.Page1Selections.Add(new ViewItem() { ItemId = 1, IsSelected = true });
myVm.Page2Selections = new List<ViewItem>();
myVm.Page2Selections.Add(new ViewItem() { ItemId = 2, IsSelected = true });
Mapper.Initialize(cfg =>
{
cfg.BindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
cfg.CreateMap<MyViewModel, MyDbModel>();
cfg.CreateMap<ViewItem, IMyDbItem>();
});
MyDbModel myDb = new MyDbModel();
myDb = Mapper.Map<MyDbModel>(myVm); //<== exception here
Sorry for the long attachment. But I'm fighting both an object to interface and hierarchical mismatch mapping problem.
Here's one way you could accomplish this:
cfg.CreateMap<MyViewModel, MyDbModel>()
.ForMember(dest => dest.DbModelId, opt => opt.MapFrom(src => src.ViewModelId));
cfg.CreateMap<ViewItem, IMyDbItem>()
.ConstructUsing((ViewItem src) => new MyDbItem());
cfg.CreateMap<IList<ViewItem>, IPageSelection>()
.ConstructUsing((IList<ViewItem> src) => new PageSelection())
.ForMember(dest => dest.PageSelections, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.PageNumber, opt => opt.Ignore());
The error you were getting is because no mapping existed from IList<ViewItem> to IPageSelection. I've created that mapping above.
An important bit is the ConstructUsing calls. I added those here because I assumed you wanted to use the concrete types that implement the interfaces you're mapping to. Otherwise, AutoMapper will create a proxy class that implements the destination interface.
The part that enables mapping to the IPageSelection.PageSelections from IList<ViewItem> is the opt.MapFrom(src => src) part. This essentially tells AutoMapper to map from the list to the inner PageSelections property, ultimately using the mapping from ViewItem to IMyDbItem.
Example: https://dotnetfiddle.net/DW9dfO
When I map a source object into an existing instance it appears to want to create a new object and not copy into...
Here is my test scenario:
List<DomainCustomerProxy> domainCustomers = customers.Select(customer => new DomainCustomerProxy(
new Lazy<List<DomainContact>>(() => RetrieveContactsByCustomerId(customer.Id, lazyLoad, schemaName).ToList(), LazyThreadSafetyMode.ExecutionAndPublication),
new Lazy<List<DomainDepartment>>(() => RetrieveDepartmentsByCustomerId(customer.Id, lazyLoad, schemaName).ToList(), LazyThreadSafetyMode.ExecutionAndPublication),
new Lazy<List<DomainGroup>>(() => RetrieveCustomerGroupsByCustomerId(customer.Id, lazyLoad, schemaName).ToList(), LazyThreadSafetyMode.ExecutionAndPublication)
)
{
Id = customer.Id,
}).ToList();
domainCustomers = Mapper.Map(customers, domainCustomers);
return domainCustomers;
I get this error:
Type 'Raven.Lib.Domain.CRM.Models.CustomerProxy' does not have a default constructor
public class CustomerProxy : Customer
{
public CustomerProxy(Lazy<List<Contact>> contacts, Lazy<List<Department>> departments, Lazy<List<Group>> groups)
{
_contactLazyList = contacts;
_departmentLazyList = departments;
_groupLazyList = groups;
}
private readonly Lazy<List<Contact>> _contactLazyList;
public override List<Contact> Contacts
{
get { return _contactLazyList.Value; }
}
private readonly Lazy<List<Department>>_departmentLazyList;
public override List<Department> Departments
{
get { return _departmentLazyList.Value; }
}
private readonly Lazy<List<Group>> _groupLazyList;
public override List<Group> CustomerGroups
{
get { return _groupLazyList.Value; }
}
}
customer base class:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public int CreatedById { get; private set; }
public int ModifiedById { get; private set; }
public DateTime DateCreated { get; private set; }
public DateTime DateModified { get; private set; }
public int Version { get; private set; }
public virtual List<Contact> Contacts { get; set; }
public virtual List<Department> Departments { get; set; }
public virtual List<Group> CustomerGroups { get; set; }
public SerializableDictionary<string, string> ValidationErrors { get; set; }
public bool Validate()
{
ValidationErrors = new SerializableDictionary<string, string>();
if (string.IsNullOrWhiteSpace(Name))
{
ValidationErrors.Add(LambdaHelper<Customer>.GetPropertyName(x => x.Name), "Customer.Name is a mandatory field.");
}
if (Number <= 0)
{
ValidationErrors.Add(LambdaHelper<Customer>.GetPropertyName(x => x.Number), "Customer.Number is a mandatory field and can not be 0.");
}
return ValidationErrors.Count > 0;
}
}
Now if I create a empty default constructor
protected CustomerProxy(){ }
It does not error but the appears to wipe out my old instance here is the proof.
Mapping ignoring the properties I dont want overwritten.
CreateMap<DaoCustomer, DomainCustomerProxy>()
.ForMember(dest => dest.Contacts, opt => opt.Ignore())
.ForMember(dest => dest.CustomerGroups, opt => opt.Ignore())
.ForMember(dest => dest.Departments, opt => opt.Ignore())
.ForMember(dest => dest.ValidationErrors, opt => opt.Ignore());
Before:
After:
Am I using automapper incorrectly?
domainCustomers = Mapper.Map(customers, domainCustomers);
Little back ground:
I am using the virtual proxy pattern for lazy loading my contacts,departments, and groups as these are very large lists. This is why I inherit from Customer which is the base class.