AutoMapper Infinite Loop using EF Code First - c#

I have the following classes (One-One relationship Asset-TrackingDevice):
public class Asset
{
public int Id { get; set; }
public string Name { get; set; }
public TrackingDevice TrackingDevice { get; set; }
}
public class TrackingDevice
{
public int Id { get; set; }
public string Imei { get; set; }
public int? AssetId { get; set; }
public Asset Asset { get; set; }
}
The viewModels are very similar:
public class AssetViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int? TrackingDeviceId { get; set; }
public TrackingDeviceViewModel TrackingDevice { get; set; }
}
public class TrackingDeviceViewModel
{
public int Id { get; set; }
public string Imei { get; set; }
public AssetViewModel Asset { get; set; }
public string AssetId { get; set; }
}
Mappings:
CreateMap<Asset, AssetViewModel>()
.ForMember(d => d.TrackingDevice, map => map.Ignore());
CreateMap<AssetViewModel, Asset>()
.ForMember(d => d.TrackingDevice, map => map.Ignore());
CreateMap<AssetViewModel, Asset>()
.ReverseMap();
CreateMap<TrackingDevice, TrackingDeviceViewModel>()
.ForMember(d => d.Asset, map => map.Ignore());
CreateMap<TrackingDeviceViewModel, TrackingDevice>()
.ForMember(d => d.Asset, map => map.Ignore());
CreateMap<TrackingDevice, TrackingDeviceViewModel>()
.ReverseMap();
When I perform a database query to obtain the TrackingDevices,
I get an error because in the mapping the Asset within Tracking Device also includes a Tracking Device and so on.
The query that I execute to obtain the tracking devices is:
var trackingDevices = _appContext.TrackingDevices
.Include(td => td.Asset)
.ToListAsync();
var trackingMapper = Mapper.Map<IEnumerable<TrackingDeviceViewModel>>(trackingDevices);
I read that by including the Map.Ignore would fix the problem but it did not work either, does anyone know what my error is?

Related

AutoMapperMappingException Strange Behaviour

I've found some strange behaviour regarding AutoMapper when I'm trying to map from this class
public class SkinAnalyzerResult {
public Guid Id { get; set; }
public ICollection<SkinAnalyzerQuestionAnswer> SelectedAnswers { get; set; }
public string Description { get; set; }
public ICollection<Product> RecommendedProducts { get; set; }
}
public class SkinAnalyzerQuestionAnswer {
public Guid Id { get; set; }
public String Answer { get; set; }
public ICollection<SkinAnalyzerResult> Results { get; set; }
public SkinAnalyzerQuestion Question { get; set; }
}
to this class
public class SkinAnalyzerResultDataModel {
[Key] public Guid Id { get; set; }
[Required] public ICollection<SkinAnalyzerResultQuestionAnswerDataModel> SelectedAnswers { get; set; }
public string Description { get; set; }
public ICollection<SkinAnalyzerResultProductDataModel> RecommendedProducts { get; set; }
public SkinAnalyzerDataModel SkinAnalyzer { get; set; }
}
public class SkinAnalyzerResultQuestionAnswerDataModel {
public Guid ResultId { get; set; }
public SkinAnalyzerResultDataModel Result { get; set; }
public Guid QuestionAnswerId { get; set; }
public SkinAnalyzerQuestionAnswerDataModel QuestionAnswer { get; set; }
}
public class SkinAnalyzerQuestionAnswerDataModel {
[Key] public Guid Id { get; set; }
[Required] public String Answer { get; set; }
public SkinAnalyzerQuestionDataModel Question { get; set; }
public ICollection<SkinAnalyzerResultQuestionAnswerDataModel> Results { get; set; }
}
using this config map
cfg.CreateMap<SkinAnalyzerResultDataModel, SkinAnalyzerResult>()
.ForMember(dest => dest.SelectedAnswers,
opt => opt.MapFrom(
src => src.SelectedAnswers.Select(x => x.QuestionAnswer)
))
.ForMember(dest => dest.RecommendedProducts,
opt => opt.MapFrom(
src => src.RecommendedProducts.Select(x => x.Product)
))
.ReverseMap();
When I tried to do that I got this AutoMapperMappingException
as you can see I've specified to map SkinAnalyzerResult.Answers into SkinAnalyzerResultDataModel.SelectedAnswers.QuestionAnswer
is there something that I missed or do something wrong?
cfg.CreateMap<SkinAnalyzerResultDataModel, SkinAnalyzerResult>()
.ForMember(dest => dest.SelectedAnswers,
opt => opt.MapFrom(
src => src.SelectedAnswers.Select(x => x.QuestionAnswer)
))
The type of SelectedAnswers is ICollection<SkinAnalyzerQuestionAnswer> while the type of QuestionAnswer is SkinAnalyzerQuestionAnswerDataModel, so you also need to create mapping between the two models:
CreateMap<SkinAnalyzerQuestionAnswerDataModel, SkinAnalyzerQuestionAnswer>()
.ForMember( //config if necessary)

How to flattening child object used repeatedly in C# with Automapper

Using the following entities
public class User
{
public Guid Id { get; set; }
public string Username { get; set; }
}
public class GeneralEntity
{
public Guid Id { get; set; }
public User CreatedByUser { get; set; }
public User DeletedByUser { get; set; }
}
How do I flatten this to the GeneralEntityDto below?
public class GeneralEntityDto
{
public Guid Id { get; set; }
public string CreatedByUsername { get; set; }
public string DeletedByUsername { get; set; }
}
I have tried setting up my mappings as seen below but it fails with a complaint about "CreatedByUsername" and "DeletedByUsername" not being mapped.
protected void Configure()
{
CreateMap<GeneralEntity, GeneralEntityDto>()
.ForMember(dest => dest.CreatedByUsername,
opt => opt.MapFrom(src => src.CreatedByUser.Username))
.ForMember(dest => dest.DeletedByUsername, opt =>
opt.MapFrom(src => src.DeletedByUser.Username));
}
You can use the naming convention that automapper provides.
Basically if you include the exact string of the property name of the source Object you do not have to add ForMember() automapper is clever enough to do it automatically.
That means for example :
public class GeneralEntity
{
public Guid Id { get; set; }
public User CreatedBy { get; set; } // renaming just for simplicity
public User DeletedBy { get; set; } // renaming just for simplicity
}
public class GeneralEntityDto
{
public Guid Id { get; set; }
public string CreatedByUsername { get; set; }
public string DeletedByUsername { get; set; }
}
Reference also to these:
http://docs.automapper.org/en/stable/Flattening.html
AutoMapper TwoWay Mapping with same Property Name

Is there a way to do the mapping using Automapper

The TeacherSubjects list in TeacherVM always shows null even though Automapper is used to map SubjectVM to TeacherSubject.
I have tried the code below with the automapper configuration. SchoolName is pulling through but TeacherSubjectlist is always null.
public class Teacher
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public School WorkingSchool { get; set; }
public int SchoolId { get; set; }
public List<TeacherSubject> TeacherSubjectslist { get; set; }
}
public class TeacherSubject
{
public int TeacherSubjectId { get; set; }
public Subject Subject { get; set; }
public int SubjectId { get; set; }
public Teacher Teacher { get; set; }
public int TeacherId { get; set; }
}
public class TeacherVM
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public int SchoolId { get; set; }
public string SchoolName { get; set; }
public List<SubjectVM> TeacherSubjects { get; set; }
}
public class SubjectVM
{
public string SubjectName { get; set; }
public int SubjectId { get; set; }
}
CreateMap<domain.TeacherSubject, SubjectVM>()
.ForMember(dest => dest.SubjectName, opt => opt.MapFrom(src =>
src.Subject.SubjectName))
.ForMember(dest => dest.SubjectId, opt => opt.MapFrom(src =>
src.Subject.SubjectId));
CreateMap<domain.Teacher, TeacherVM>()
.ForMember(dest => dest.SchoolName, opt => opt.MapFrom(src =>
src.WorkingSchool.SchoolName))
.ForMember(dest => dest.TeacherSubjects, opt => opt.MapFrom(src
=> src.TeacherSubjectslist));
TeacherSubjectlist should be a list of the SubjectId and the SubjectName properties.
The problem is that you are missing creating instance of list types inside each class that contains list of objects.
What you need to do is add instance of list type in class constructor.
With out testing it, for example:
public Teacher()
{
TeacherSubjectslist = new List<TeacherSubject>();
..
..
The same concept is valid for the remaining classes that have list of objects.

Entity Framework Core Navigation Properties Error

I'm trying to make a simple app to try Entity Framework Core, but i a have problem with setting up relations between entities. My entities:
public class Card
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Adress { get; set; }
public DateTime DoB { get; set; }
public DateTime DoS { get; set; }
public User Portal { get; set; }
public List<Reservation> Res { get; set; }
}
public class Doctor
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public TimeSpan Start_Working { get; set; }
public TimeSpan End_Working { get; set; }
public List<Reservation> Reservations { get; set; }
public int SpecID { get; set; }
public Spec Spec { get; set; }
}
public class Reservation
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime DoR { get; set; }
public string Info { get; set; }
public int CardID { get; set; }
public Card Card_Nav_R { get; set; }
public int DoctorID { get; set; }
public Doctor Doctor { get; set; }
}
public class Spec
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public List<Doctor> Doctors { get; set; }
}
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int CardID { get; set; }
public Card Card { get; set; }
}
And a configuration class where i tried to set up relations:
class ApplicationContext:DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Card> Cards { get; set; }
public DbSet<Reservation> Reservations { get; set; }
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Spec> Specs { get; set; }
public ApplicationContext()
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder ModelBuilder)
{
ModelBuilder.Entity<User>().HasKey(u => u.Id);
ModelBuilder.Entity<Card>().HasKey(c => c.Id);
ModelBuilder.Entity<Doctor>().HasKey(d => d.Id);
ModelBuilder.Entity<Spec>().HasKey(s => s.Id);
ModelBuilder.Entity<Reservation>().HasKey(r => r.Id);
ModelBuilder.Entity<User>().Property(u => u.Email).IsRequired();
ModelBuilder.Entity<User>().Property(u => u.Password).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Name).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Surname).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.DoB).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Adress).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Name).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Surname).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Email).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Start_Working).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.End_Working).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Info).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Card_Nav_R).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Doctor).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.DoR).IsRequired();
ModelBuilder.Entity<Spec>().Property(s => s.Name).IsRequired();
ModelBuilder.Entity<Doctor>().HasOne<Spec>(d=>d.Spec).WithMany(s => s.Doctors).HasForeignKey(d => d.SpecID);
ModelBuilder.Entity<User>().HasOne<Card>(u => u.Card).WithOne(c => c.Portal).HasForeignKey<User>(u => u.CardID);
ModelBuilder.Entity<Reservation>().HasOne<Card>(r => r.Card_Nav_R).WithMany(c => c.Res).HasForeignKey(r => r.CardID);
ModelBuilder.Entity<Reservation>().HasOne<Doctor>(r => r.Doctor).WithMany(d => d.Reservations).HasForeignKey(r => r.DoctorID);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Simple_Try;Trusted_Connection=True;");
}
}
So, when i tried to add migration or add something to database i saw this error:
System.InvalidOperationException: 'The property or navigation 'Spec' cannot be added to the entity type 'Doctor' because a property or navigation with the same name already exists on entity type 'Doctor'.'
I really don't know how to fix this, i tried to use annotations instead of Fluent API, but had the same result.
The cause of the exception is the following line:
ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();
because Doctor.Spec is a navigation property
public class Doctor
{
// ...
public Spec Spec { get; set; }
}
and navigation properties cannot be configured via Property fluent API.
So simply remove that line. Whether reference navigation property is required or optional is controlled via relationship configuration. In this case
ModelBuilder.Entity<Doctor>()
.HasOne(d => d.Spec)
.WithMany(s => s.Doctors)
.HasForeignKey(d => d.SpecID)
.IsRequired(); // <--
although the IsRequired is automatically derived from the FK property type - since SpecID is non nullable, then the relationship is required.
For more info, see Required and Optional Properties and Required and Optional Relationships documentation topics.

Automapper complex object mapping NullReference exception

I have the following classes
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<GroupTier> Tiers { get; set; }
}
public class GroupTier : IEntity
{
public int Id { get; set; }
public int GroupId { get; set; }
public int Tier { get; set; }
public decimal Amount { get; set; }
public virtual Group Group { get; set; }
}
I am trying to map to the following ViewModel
public class GroupViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<decimal> Tiers { get; set; }
}
using the configuration
configuration.CreateMap<Group, GroupViewModel>()
.ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));
I am using EF6 to query from the database. I am having trouble when the Group.Tiers is null. How can I handle the null value?
When I use the this configuration
configuration.CreateMap<Group, GroupViewModel>()
.ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers == null ? new List<decimal>() : u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));
I am getting this error
Cannot compare elements of type 'System.Collections.Generic.ICollection'

Categories