Automapper returning "default" instance of mapped entity - c#

I'm trying to get automapper working for .NET core, and I seem to be missing something important.
I have my automapper set up as follows:
In Startup.cs in my ConfigureServices method, I have the following line:
services.AddAutoMapper(typeof(Startup), typeof(ApplicationDbContext));
I also have a class called MappingProfile which resembles this:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<CfGroup, GroupModel>()
.IgnoreAllUnmapped();
CreateMap<GroupModel, CfGroup>()
.IgnoreAllUnmapped();
...
}
}
The IgnoreAllUnmapped was suggested by another SO question, and it looks like this:
public static IMappingExpression<TSource, TDest> IgnoreAllUnmapped<TSource, TDest>(
this IMappingExpression<TSource, TDest> expression)
{
expression.ForAllMembers(opt => opt.Ignore());
return expression;
}
My CfGroup looks like this:
[Table("Groups")]
public class CfGroup
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(100)]
[Required]
public string Name { get; set; }
[DataType(DataType.ImageUrl)]
[StringLength(250)]
public string ImageUrl { get; set; }
[Required]
public int FounderId { get; set; }
[ForeignKey("FounderId")]
public virtual CfUser Founder { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public DateTime CreatedTimestampUtc { get; set; }
[StringLength(3000)]
public string About { get; set; }
public virtual ISet<UserGroupMember> Members { get; set; }
public virtual ISet<UserGroupManager> Managers { get; set; }
}
... and my GroupModel looks like this:
public class GroupModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public string About { get; set; }
}
When calling my web api endpoint, I get a collection of CfGroup entities from the database just fine. I then run a command like so:
var dtos = entities.Select(_mapper.Map<GroupModel>);
...but my dtos are "empty"... IOW, the instance is created, but Id value is set to 0 and all the string values are set to null. That is definitely not what is in the entities variable.
I have also tried to explicitly convert it like so:
var dtos = entities.Select(x => _mapper.Map<GroupModel>(x));
and, postulating that it's an IEnumerable issue, tried making a List out of it:
var dtos = entities.Select(x => _mapper.Map<GroupModel>(x)).ToList();
...but no matter what, the DTO's remain instantiated, but with all properties set to their default values.
What am I doing wrong?

I suspect it may be because you are mapping inside your linq expression. try something like this:
var dtos = _mapper.Map<IEnumerable<GroupModel>>(entities.ToList());

So it turns out that the IgnoreAllUnmapped was not the correct way to go; in fact, the mapping seemed to work fine without it. I took out the extra "exception handling" code and it worked as I wanted it to. I was overthinking it.

Related

AutoMapper postfix not recognized

I'm using AutoMapper v9, and wish to have a postfix on my DTO property names of Dto.
In my profile I add:
RecognizeDestinationPostfixes("Dto");
I validate my mapping with config.AssertConfigurationIsValid(); and immediately it throws with
Unmapped properties:
AccountConnectionIdDto
CreatedDateDto
ModifiedDateDto
IdentityIdDto
AccountIdDto
This is what my DTO looks like:
public class AccountConnectionDto
{
public int AccountConnectionIdDto { get; set; }
public DateTime CreatedDateDto { get; set; }
public DateTime ModifiedDateDto { get; set; }
public Guid IdentityIdDto { get; set; }
public int AccountIdDto { get; set; }
public AccountDto AccountDto { get; set; }
}
Removing the Dto from my model names, and it all works. I feel like the postifx isn't being recognized or utilized at all, am I doing it the correct way?

EF Core Many To Many. Error returning entities

I'm implementing an WebAPI using dotnet core (2) and EF Core. The application uses UnitOfWork/Repository-pattern. I've come to a point where I need to implement a "Many-To-Many"-relation but I'm having some trouble. This is what I've got sofar:
Entities:
public class Team : DataEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TeamId { get; set; }
public int OrganizationID { get; set; }
public string Name { get; set; }
public string URL { get; set; }
public virtual ICollection<Season> Seasons { get; set; }
public ICollection<TeamMember> TeamMember { get; set; }
}
public class Member : DataEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int MemberId { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public int RelatedTo { get; set; }
public int TeamRole { get; set; }
public ICollection<TeamMember> TeamMember { get; set; }
}
public class TeamMember
{
public int TeamId { get; set; }
public Team Team { get; set; }
public int MemberId { get; set; }
public Member Member { get; set; }
}
In my DbContext-class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TeamMember>()
.HasKey(t => new { t.TeamId, t.MemberId });
modelBuilder.Entity<TeamMember>()
.HasOne(tm => tm.Team)
.WithMany(t => t.TeamMember)
.HasForeignKey(tm => tm.TeamId);
modelBuilder.Entity<TeamMember>()
.HasOne(tm => tm.Member)
.WithMany(t => t.TeamMember)
.HasForeignKey(tm => tm.MemberId);
}
On my repository I have en extension for IncludeAll:
public static IQueryable<Team> IncludeAll(this IGenericRepository<Team> repository)
{
return repository
.AsQueryable()
.Include(s => s.Seasons)
.Include(tm => tm.TeamMember);
}
Everything builds as expected but when trying to invoke the controller-action that's fetching a Team (which I expect to include all members) or a Member (which I expect to include all the members teams - code not included above). SwaggerUI returns: TypeError: Failed to fetch. If I try to invoke the controller-action directly in chrome (http://localhost/api/Team/1) i get an incomplete result, but never the less...a result :) :
{
"value":
{
"teamId":1,
"organizationID":1,
"name":"Team1",
"url":"http://www.team1.com",
"seasons":[
{
"id":1,
"teamID":1,
"name":"PK4",
"description":null,
"startDate":"2017-09-01T00:00:00",
"endDate":"2018-12-31T00:00:00",
"created":"2017-12-01T00:00:00",
"updated":"2017-12-27T00:00:00",
"createdBy":"magnus",
"updatedBy":"magnus"
}],
"teamMember":[
{
"teamId":1
Am I missing something obvious?
Might be related to some circular reference issue?!
your teamMember property has an Team property which again has a teamMember properties and so on... When trying to serialize, a never ending loop would be created.
Maybe you can set the teamMember.Team property to null. I think you would also have to set the teamMember.Member.teamMember property to null.
You have a reference loop in your data structure (team -> team member -> team), and the JSON encoder can't deal with this.
I can think of two ways to solve this:
Configure the JSON encoder to ignore reference loops
In Startup.cs, add this:
services
.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
From the documentation:
ReferenceLoopHandling.Ignore: Json.NET will ignore objects in reference loops and not serialize them. The first time an object is encountered it will be serialized as usual but if the object is encountered as a child object of itself the serializer will skip serializing it.
Add the [JsonIgnore] attribute to the property you want to ignore
For example:
public class TeamMember
{
public int TeamId { get; set; }
[JsonIgnore]
public Team Team { get; set; }
}

Converting infinitely nested objects in .NET Core

EDIT: I originally worded this question very poorly, stating the problem was with JSON serialization. The problem actually happens when I'm converting from my base classes to my returned models using my custom mappings. I apologize for the confusion. :(
I'm using .NET Core 1.1.0, EF Core 1.1.0. I'm querying an interest and want to get its category from my DB. EF is querying the DB properly, no problems there. The issue is that the returned category has a collection with one interest, which has one parent category, which has a collection with one interest, etc. When I attempt to convert this from the base class to my return model, I'm getting a stack overflow because it's attempting to convert the infinite loop of objects. The only way I can get around this is to set that collection to null before I serialize the category.
Interest/category is an example, but this is happening with ALL of the entities I query. Some of them get very messy with the loops to set the relevant properties to null, such as posts/comments.
What is the best way to address this? Right now I'm using custom mappings that I wrote to convert between base classes and the returned models, but I'm open to using any other tools that may be helpful. (I know my custom mappings are the reason for the stack overflow, but surely there must be a more graceful way of handling this than setting everything to null before projecting from base class to model.)
Classes:
public class InterestCategory
{
public long Id { get; set; }
public string Name { get; set; }
public ICollection<Interest> Interests { get; set; }
}
public class Interest
{
public long Id { get; set; }
public string Name { get; set; }
public long InterestCategoryId { get; set; }
public InterestCategory InterestCategory { get; set; }
}
Models:
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
public List<InterestModel> Interests { get; set; }
}
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
public InterestCategoryModel InterestCategory { get; set; }
public long? InterestCategoryId { get; set; }
}
Mapping functions:
public static InterestCategoryModel ToModel(this InterestCategory category)
{
var m = new InterestCategoryModel
{
Name = category.Name,
Description = category.Description
};
if (category.Interests != null)
m.Interests = category.Interests.Select(i => i.ToModel()).ToList();
return m;
}
public static InterestModel ToModel(this Interest interest)
{
var m = new InterestModel
{
Name = interest.Name,
Description = interest.Description
};
if (interest.InterestCategory != null)
m.InterestCategory = interest.InterestCategory.ToModel();
return m;
}
This is returned by the query. (Sorry, needed to censor some things.)
This is not .NET Core related! JSON.NET is doing the serialization.
To disable it globally, just add this during configuration in Startup
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}));
edit:
Is it an option to remove the circular references form the model and have 2 distinct pair of models, depending on whether you want to show categories or interests?
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
public List<InterestModel> Interests { get; set; }
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
}
}
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
public InterestCategoryModel InterestCategory { get; set; }
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
}
}
Note that each of the models has a nested class for it's child objects, but they have their back references removed, so there would be no infinite reference during deserialization?

Dynamic mapping using AutoMapper

I have two classes:
public class ClassA
{
public ClassA()
{
LanguageInfo = new List<ClassALanguage>();
}
public long ID { get; set; }
public string Value { get; set; }
public List<ClassALanguage> LanguageInfo { get; set; }
}
public class ClassALanguage
{
public long ID { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
And I have a destination class:
public class WSClassA
{
public long ID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
So I thought I could configure the mapping like this:
Mapper.CreateMap<ClassA, WSClassA>()
.ForMember(
ws => ws.Name,
opt => opt.MapFrom(clsA => clsA.LanguageInfo.Find(lg => lg.Language == languageSelected).Name));
The problem is after the first time the mapping is executed, it is not able to change. Even if the CreateMap() is executed with another value for languageSelected, the binding works as the first time.
Is there any solution to accomplish that?
In your case you need some context when you perform your mapping - the selected language.
Instead of using MapFrom use the ResolveUsing method with a custom IValueResolver.
See - https://automapper.codeplex.com/wikipage?title=Custom%20Value%20Resolvers
When you make your .Map call use the overload which allows you to modify the IMappingOperationOptions. Set the language you want to compare against in the interfaces Items collection.
Finally in your custom IValueResolver's Resolve method you can access those items via ResolutionResult.Context.Options.Items property.

Automapper forsourcemember ignore not working

I am pulling from an entity object and trying to map to a model. Here is my class information:
// entity class was auto-generated
**public partial class CategoryType
{
public CategoryType()
{
this.OutgoingCategories = new HashSet<OutgoingCategory>();
}
public int CategoryTypeID { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public virtual ICollection<OutgoingCategory> OutgoingCategories { get; set; }
}**
public class CategoryTypeModel
{
public int CategoryTypeID { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
}
My mapper looks like this:
Mapper.CreateMap<CategoryType, CategoryTypeModel>().ForSourceMember(a => a.OutgoingCategories, o => o.Ignore());
When I go to get the mapped list:
Mapper.Map <List<CategoryType>, List<CategoryTypeModel>>(dbo.CategoryTypes.ToList());
I get the error:
Missing type map configuration or unsupported mapping.
I'm new to using Automapper, so I'm not sure what I'm doing wrong. Any help would be great. Thanks.
Well, I said I was new to Automapper. So.... I guess it would help to call this in my Global.asax:
AutoMapperConfig.Configure();
That solved the problem.

Categories