AutoMapper Writing Null On Target Field When Source Field Does Not Exist - c#

I have the following objects:
public class DomainStudent {
public long Id { get; set; }
public string AdvisorId { get; set; }
}
public class ApiStudent {
public long Id { get; set; }
public long AdvisorName { get; set; }
}
When I run the following mapping:
ApiStudent api = new ApiStudent();
api.Id = 123;
api.AdvisorName = "Homer Simpson";
DomainStudent existing = service.load(api.Id); // 123
// at this point existing.AdvisorId = 555
existing = Mapper.Map<ApiStudent, DomainStudent>(api);
// at this point existing.AdvisorId = null
How can I configure AutoMapper such that when the property AdvisorId is missing from the source object, so that it does not get overwritten to null?

You must change the Map() call to:
Mapper.Map(api, existing);
and then configure the mapping to:
Mapper.CreateMap<ApiStudent, DomainStudent>()
.ForMember(dest => dest.AdvisorId, opt => opt.Ignore());

Related

AutoMapper - Get error when trying to map two classes

I am trying to use AutoMapper to map a DTO to an Entity class but I keep getting an error.
Here is the DTO Class:
public class Product
{
public string ID { get; set; }
public string SKU { get; set; }
public string Name { get; set; }
public PriceTiers PriceTiers { get; set; }
}
and here is the Entity:
public partial class Product
{
public Product()
{
PriceTiers = new List<PriceTiers>();
}
[Key]
public string ID { get; set; }
public string SKU { get; set; }
public string Name { get; set; }
public virtual ICollection<PriceTiers> PriceTiers { get; set; }
}
Why do I keep getting the following error?
{"Missing type map configuration or unsupported
mapping.\r\n\r\nMapping types:\r\nPriceTiers ->
ICollection1\r\nWeb.Areas.DEAR.DTOs.PriceTiers -> System.Collections.Generic.ICollection1[[Web.Areas.DEAR.Data.PriceTiers,
Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\r\n\r\n
Destination Member:\r\nPriceTiers\r\n"}
This is what I have in the Profile class:
AllowNullCollections = true;
CreateMap<DTOs.Product, Data.Product>();
CreateMap<DTOs.PriceTiers, Data.PriceTiers>();
and this is what I use to map the classes:
var products = _mapper.Map<IEnumerable<Product>>(result.Products);
This is what is in the Program.cs:
builder.Services.AddAutoMapper(typeof(AutoMapperProfiles).Assembly);
The exception message is quite clear, the AutoMapper doesn't know how to map the data from DTOs.PriceTiers to ICollection<Data.PriceTiers>.
Solution 1: Map from DTOs.PriceTiers to ICollection<Data.PriceTiers>
I believe that Custom Type Converters is what you need.
Create Custom Type Converters.
public class ICollectionDataPriceTiersTypeConverter : ITypeConverter<DTOs.PriceTiers, ICollection<Data.PriceTiers>>
{
public ICollection<Data.PriceTiers> Convert(DTOs.PriceTiers src, ICollection<Data.PriceTiers> dest, ResolutionContext context)
{
if (src == null)
return default;
var singleDest = context.Mapper.Map<Data.PriceTiers>(src);
return new List<Data.PriceTiers>
{
singleDest
};
}
}
Add to mapping profile.
CreateMap<DTOs.PriceTiers, ICollection<Data.PriceTiers>>()
.ConvertUsing<ICollectionDataPriceTiersTypeConverter>();
Demo # .NET Fiddle
Solution 2: Map from ICollection<DTOs.PriceTiers> to ICollection<Data.PriceTiers>
If the PriceTiers in DTOs.Product supports multiple items and mapping with many to many (to ICollection<Data.ProductTiers>), then consider modifying the property as the ICollection<DTOs.PriceTiers> type.
namespace DTOs
{
public class Product
{
...
public ICollection<PriceTiers> PriceTiers { get; set; }
}
}
Did you added "CreateMapper()" method after your configurations?
Try something like that.
public class MappingProfile : Profile
{
public MappingProfile {
AllowNullCollections = true;
CreateMap<DTOs.Product, Data.Product>();
CreateMap<DTOs.PriceTiers, Data.PriceTiers>();
}
}
After that, on your container service, inject this dependency:
var mappingConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
After some more research I found out that my mapping profile was not in the right order. These are the changes I made.
public class AutoMapperProfiles : Profile
{
public AutoMapperProfiles()
{
AllowNullCollections = true;
CreateMap<DTOs.PriceTiers, Data.PriceTiers>();
CreateMap<DTOs.Product, Data.Product>()
.ForMember(dto => dto.PriceTiers, opt => opt.MapFrom(x => x.PriceTiers));
}
}
Now it maps perfectly

AutoMapper - Inheritance preserve reference

I have the following scenario
Entity framework classes classes:
public class Block
{
public Guid Id { get; set; }
public ICollection<BlockLocation> BlockLocations { get; set; }
public BlockType Type { get; set; }
}
public class BlockLocation
{
public Guid Id { get; set; }
public Guid BlockId { get; set; }
public Block Block { get; set; }
}
And my Domain Entities look like
public class Block
{
public Block(BlockType type = BlockType.None) : this()
{
Type = type;
}
private Block() { }
public Guid Id { get; set; }
public List<BlockLocation> BlockLocations { get; set; }
public BlockType Type { get; set; }
}
public class LiveBlock : Block
{
public LiveBlock() : base(BlockType.Live) { }
}
public class UnsequencedBlock : Block
{
public UnsequencedBlock() : base(BlockType.Unsequenced) { }
}
public class BlockLocation
{
public Guid Id { get; set; }
public Guid BlockId { get; set; }
public Block Block { get; set; }
}
public enum BlockType
{
None = 0,
Live,
Unsequenced
}
And what I want to do is map from Entity Framework to a Domain entity to the child type and also preserve the reference so that I don't get a stack overflow
My mappings are
cfg.CreateMap<Data.Block, Domain.LiveBlock>();
cfg.CreateMap<Data.Block, Domain.UnsequencedBlock>();
cfg.CreateMap<Data.Block, Domain.Block>().PreserveReferences().ConstructUsing((block, context) =>
{
if (block.Type == BlockType.Live)
{
// This loops until stack overflow
return context.Mapper.Map<Domain.LiveBlock>(block);
}
if (block.Type == BlockType.Unsequenced)
{
return context.Mapper.Map<Domain.LiveBlock>(block);
}
return context.Mapper.Map<Domain.Block>(block);
});
cfg.CreateMap<Data.BlockLocation, Domain.BlockLocation>();
And I'm trying to do the following:
// This is the EF entity
var block = new Data.Block
{
Id = Guid.NewGuid(),
Type = BlockType.Live,
BlockLocations = new List<Data.BlockLocation>
{
new BlockLocation {Id = Guid.NewGuid()},
new BlockLocation {Id = Guid.NewGuid()}
}
};
block.BlockLocations[0].Block = block;
block.BlockLocations[1].Block = block;
// Trying to create a Domain entity
var domainBlock = Mapper.Map<Data.Block, Domain.Block>(block);
The result that I want to achieve is for domainBlock to be of type LiveBlock and have a list of BlockLocations which in turn have the same LiveBlock entity as their Block property
What I get is a loop in ConstructUsing, until I get stack overflow.
Now, my questions are:
Can this be achieved with AutoMapper?
If yes, can it be done with ContructUsing? I've also tried ConvertUsing, but I get the same result.
Some other approach maybe?
I know that a way of doing to would be to Ignore the BlockLocations property from Domain.Block and map them separately, but I would like to have Automapper to that automatically.
Thank you for your help.
Got it working with Lucian's help
I changed the mapper to the following
cfg.CreateMap<Data.Block, Domain.LiveBlock>().PreserveReferences();
cfg.CreateMap<Data.Block, Domain.UnsequencedBlock>().PreserveReferences();
cfg.CreateMap<Data.Block, Domain.Block>().PreserveReferences().ConstructUsing((block, context) =>
{
if (block.Type == BlockType.Live)
{
var b = new LiveBlock();
return context.Mapper.Map(block, b, context);
}
if (block.Type == BlockType.Unsequenced)
{
var unsequencedBlock = new UnsequencedBlock();
return context.Mapper.Map(block, unsequencedBlock, context);
}
return context.Mapper.Map<Domain.Block>(block);
});
cfg.CreateMap<Data.BlockLocation, Domain.BlockLocation>().PreserveReferences();
The secred was usint the Map method that takes the context as a parameter
context.Mapper.Map(block, unsequencedBlock, context);

Automapper nests parent entity inside child entity

I am having an issue when mapping nested classes that reference each other. Entity framework populares the nested entities in a loop so I end up with the parent nested inside the nested child entity after mapping.
I made a sample program you can copy paste that shows the problem. In my actual program I am mapping collections so it would require to loop over entire collection to set all the nested object to null and that does not feel neat, I would rather adjust my mapping rules if possible.
Here is code that shows the issue:
using AutoMapper;
public class Job
{
public string Name { get; set; }
public PayPackage PayPackage { get; set; }
}
public class PayPackage
{
public string Name { get; set; }
public Job Job { get; set; }
}
public class JobViewModel
{
public string Name { get; set; }
public PayPackageViewModel PayPackage { get; set; }
}
public class PayPackageViewModel
{
public string Name { get; set; }
public JobViewModel Job { get; set; }
}
class Program
{
static void Main(string[] args)
{
var job = new Job
{
Name = "Job Name",
PayPackage = new PayPackage
{
Name = "Pay Package Name"
}
};
job.PayPackage.Job = job; //simulate how EF is populating entity
var config = new MapperConfiguration(c =>
{
c.CreateMap<Job, JobViewModel>();
c.CreateMap<JobViewModel, Job>();
c.CreateMap<PayPackage, PayPackageViewModel>();
c.CreateMap<PayPackageViewModel, PayPackage>();
});
var mapper = config.CreateMapper();
var jobVm = mapper.Map<JobViewModel>(job);
Assert.IsTrue(jobVm.PayPackage != null);
Assert.IsTrue(jobVm.PayPackage.Job == null); //how do I specify mapping so this passes?
}
}
What is the best way to avoid the parent appearing twice in the mapped result?
E.g.
c.CreateMap<Job, JobViewModel>()
.ForMember(dest => dest.PayPackage, opt => opt.Ignore());
The PayPackage property of the destination JobViewModel object will be ignored when the mapping occurs.

MongoDB Unable to determine the serialization information for the expression error

My data is having following structure
public enum ParamType
{
Integer=1,
String=2,
Boolean=3,
Double=4
}
public class Gateway
{
public int _id { get; set; }
public string SerialNumber { get; set; }
public List<Device> Devices { get; set; }
}
public class Device
{
public string DeviceName { get; set; }
public List<Parameter> Parameters { get; set; }
}
public class Parameter
{
public string ParamName { get; set; }
public ParamType ParamType { get; set; }
public string Value { get; set; }
}
I filled 10 document objects of Gateway in a MongoDB database.
Now I want to query all those gateways which contains a device having Parameter with ParamName as "Target Temperature" and whose Value > 15.
I created following queries
var parameterQuery = Query.And(Query<Parameter>.EQ(p => p.ParamName, "Target Temperature"), Query<Parameter>.GT(p => int.Parse(p.Value), 15));
var deviceQuery = Query<Device>.ElemMatch(d => d.Parameters, builder => parameterQuery);
var finalQuery = Query<Gateway>.ElemMatch(g => g.Devices, builder => deviceQuery);
But when I run this, it is giving an exception
Unable to determine the serialization information for the expression: (Parameter p) => Int32.Parse(p.Value)
Please suggest where I am wrong.
As the error suggests, you can't use Int32.Parse inside your query. This lambda expression is used to get out the name of the property and it doesn't understand what Int32.Parse is.
If you are querying a string, you need to use a string value for comparison:
var parameterQuery = Query.And(Query<Parameter>.EQ(p => p.ParamName, "Target Temperature"), Query<Parameter>.GT(p => p.Value, "15"));
However, that's probably not what you want to do since you're using GT. To be treated as a number for this comparison you need the value to actually be an int in mongo, so you would need to change the type of your property:
public class Parameter
{
public string ParamName { get; set; }
public ParamType ParamType { get; set; }
public int Value { get; set; }
}
var parameterQuery = Query.And(Query<Parameter>.EQ(p => p.ParamName, "Target Temperature"), Query<Parameter>.GT(p => p.Value, 15));
Summary
I ran into this when I was modifying a list. It appears that Linq First/FirstOrDefault was not handled well by MongoDB for me. I changed to be an array index var update = Builders<Movie>.Update.Set(movie => movie.Movies[0].MovieName, "Star Wars: A New Hope"); Note: This is in Asp.Net 5 using MongoDB.Driver 2.2.0.
Full Example
public static void TypedUpdateExample() {
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test");
var collection = database.GetCollection<Movie>("samples");
//Create some sample data
var movies = new Movie {
Name = "TJ",
Movies = new List<MovieData>
{
new MovieData {
MovieName = "Star Wars: The force awakens"
}
}
};
collection.InsertOne(movies);
//create a filter to retreive the sample data
var filter = Builders<Movie>.Filter.Eq("_id", movies.Id);
//var update = Builders<Movie>.Update.Set("name", "A Different Name");
//TODO:LP:TSTUDE:Check for empty movies
var update = Builders<Movie>.Update.Set(movie => movie.Movies[0].MovieName, "Star Wars: A New Hope");
collection.UpdateOne(filter, update);
}
public class Movie {
[BsonId]
public ObjectId Id { get; set; }
public string Name { get; set; }
public List<MovieData> Movies { get; set; }
}
public class MovieData {
[BsonId]
public ObjectId Id { get; set; }
public string MovieName { get; set; }
}

Geo_Point Property not indexing as expected

I am using NEST .net client for Elastic Search. I have an Address class with Location Property in it.
public class Address
{
public string AddressLine1 {get;set;}
public string AddressLine2 {get;set;}
public string City {get;set;}
public string State {get;set;}
public string ZipCode {get;set;}
[ElasticProperty(Type = FieldType.geo_point)]
public GeoLocation Location {get;set;}
}
public class GeoLocation
{
public float Lat {get;set;}
public float Lon {get;set;}
}
I have decorated the Location property with ElasticProperty Attribute geo_point type. I was able to build index for this type. But When i try to get the mapping for this,
http://localhost:9200/mysite/Address/_mapping
i am expecting the Location property to be of "geo_point" type instead it shows something like this.
{
"Address": {
"properties": {
"location": {
"properties": {
"lat": {
"type": "double"
},
"lon": {
"type": "double"
}
}
}
}
}
}
Am i missing anything here?
Here is a full example of how to map a GeoPoint using Nest along with setting other properties. Hopefully helps someone in thefuture.
public class Location
{
public decimal Lat { get; set; }
public decimal Lon { get; set; }
}
public class Building : BaseEntity
{
public IEnumerable<Location> Location { get; set; }
}
public void CreateBuildingMapping()
{
var nodes = new List<Uri>() { ... };
var connectionPool = new Elasticsearch.Net.ConnectionPool.SniffingConnectionPool(nodes);
var connectionSettings = new Nest.ConnectionSettings(connectionPool, "myIndexName");
var elasticsearchClient = new Nest.ElasticClient(connectionSettings);
var putMappingDescriptor = new Nest.PutMappingDescriptor<Building>(connectionSettings);
putMappingDescriptor.DateDetection(false);
putMappingDescriptor.Dynamic(false);
putMappingDescriptor.IncludeInAll(true);
putMappingDescriptor.IgnoreConflicts(false);
putMappingDescriptor.Index("myIndexName");
putMappingDescriptor.MapFromAttributes();
putMappingDescriptor
.MapFromAttributes()
.Properties(p =>
p.GeoPoint(s =>
s.Name(n => n.Location).IndexGeoHash().IndexLatLon().GeoHashPrecision(12)
)
);
putMappingDescriptor.NumericDetection(false);
elasticsearchClient.Map(putMappingDescriptor);
}
I used Vinoths code from his comment.
Please use the MapFluent() method to specify mappings. The attribute based mapping is very limitted in what it can express. In the next release MapFluent will be renamed to Map and it will be the only way to specify the mapping.
See this example for mapping a geo_point type:
https://github.com/elasticsearch/elasticsearch-net/blob/master/src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs#L225
Create elastic client
var uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri).SetDefaultIndex("my_indexone");
var client = new ElasticClient(settings);
client.CreateIndex("my_indexone", s => s.AddMapping<VehicleRecords>(f => f.MapFromAttributes().Properties(p => p.GeoPoint(g => g.Name(n => n.Coordinate ).IndexLatLon()))));
Create below class
public class VehicleRecords
{
public string name { get; set; }
public Coordinate Coordinate { get; set; }
public double Distance { get; set; }
}
public class Coordinate
{
public double Lat { get; set; }
public double Lon { get; set; }
}
Step 2 :Insert some record using above class
Step 3 :Using below query to search....
Nest.ISearchResponse<VehicleRecords> Response = client.Search<VehicleRecords>(s => s.Sort(sort => sort.OnField(f => f.Year).Ascending()).From(0).Size(10).Filter(fil => fil.GeoDistance(n => n.Coordinate, d => d.Distance(Convert.ToDouble(100), GeoUnit.Miles).Location(73.1233252, 36.2566525))));

Categories