How to ignore properties based on their type

Viewed 1179

I'm using AutoMapper to copy an entity framework object to another identical database. The problem is that it tries to copy the lookup tables.

I have tried to exclude them with the AddGlobalIgnore and the ShouldMapProperty but it doesn't work. AutoMapper still try to copy those properties.

Here's my code. I would like to ignore the properties that start with "LU"

 dynamic newObject= new NewObject();

 MapperConfiguration config = new MapperConfiguration(cfg =>
 {
     cfg.CreateMissingTypeMaps = true;
     cfg.AddGlobalIgnore("LU");
     cfg.ShouldMapProperty = p => !p.GetType().ToString().StartsWith("LU");
     cfg.ShouldMapField = p => !p.GetType().ToString().StartsWith("LU");
 });
 IMapper mapper = config.CreateMapper();
 newObject = mapper.Map(objectToCopy, objectToCopy.GetType(), newObject.GetType()); 

I did also tried

MapperConfiguration config = new MapperConfiguration(cfg =>
{
   cfg.CreateMissingTypeMaps = true;
   cfg.AddGlobalIgnore("LU");
   cfg.ShouldMapProperty = p => !p.PropertyType.Name.StartsWith("LU");
   cfg.ShouldMapField = p => !p.FieldType.Name.StartsWith("LU");
 });

and

MapperConfiguration config = new MapperConfiguration(cfg =>
{
   cfg.CreateMissingTypeMaps = true;
   cfg.AddGlobalIgnore("LU");
   cfg.ShouldMapProperty = p => !p.Name.StartsWith("LU");
   cfg.ShouldMapField = p => !p.Name.StartsWith("LU");
});
2 Answers
Related