I am using Automapper to map my Model objects to DTO. In DTO the primary key should be replaced with the corresponding object. For this purpose I used the code below:
// Model class
public class SubDepartment
{
public long Id { get; set; }
public string Name { get; set; }
public long? DepartmentId { get; set; }
public DateTime LastUpdated { get; set; }
}
// DTO class
public class SubDepartmentDTO
{
public long Id { get; set; }
public string Name { get; set; }
public Department Department { get; set; }
public long EventCount { get; set; }
}
// Mapping code
Mapper.CreateMap<Models.Event.SubDepartment, DTO.SubDepartment>().ForMember(dto => dto.Department,
map => map.MapFrom(sd => Mapper.Map<Department, DTO.Department>(_departmentRepository.GetById(sd.DepartmentId.Value))));
But when I map from SubDepartment to SubDepartmentDTO in my controller, the 'Department' object is always null. I tried replacing the _departmentRepository.GetById(sd.DepartmentId.Value) code with a hardcoded Department object and it is working good. I also verified there is a corresponding Department exist in the database for the primary key. Can anyone point out what I am doing wrong ?
Try it with this code
Mapper.CreateMap<Models.Event.SubDepartment, DTO.SubDepartment>().ForMember(dto => dto.Department,map => map.MapFrom(sd => _departmentRepository.GetById(sd.DepartmentId.Value)));
if it doesn't work you could try Custom value resolver
Models.Event.SubDepartment, DTO.SubDepartment>().ForMember(dto => dto.Department, map => map.ResolveUsing<DepartmentResolver>());
public class DepartmentResolver: ValueResolver<Models.Event.SubDepartment,DTO.SubDepartment>
{
Reporsitory _departmentRepository;
protected override DTO.SubDepartment ResolveCore(Models.Event.SubDepartment source)
{
return _departmentRepository.GetById(source.DepartmentId.Value);
}
}
Related
This is my class which holds database data:
public partial class PermissionGroup
{
public int Id { get; set; }
public string Name { get; set; }
// other database properties
public virtual ICollection<GroupActionPermission> GroupActionPermissions { get; set; }
}
And that's my dto's:
public class PermissionGroupDTO
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<GroupActionPermissionDTO> ActionPermissions { get; set; }
}
public class GroupActionPermissionDTO
{
public int Id { get; set; }
public int GroupId { get; set; }
public int PermissionActionId { get; set; }
public PermissionGroupDTO Group { get; set; }
}
Now, I am making mapping:
public IEnumerable<PermissionGroupDTO> GetGroups()
{
return OnConnect<IEnumerable<PermissionGroupDTO>>(db =>
{
return db.PermissionGroups
.Include(i => i.GroupActionPermissions)
.ProjectTo<PermissionGroupDTO>()
.ToList();
});
}
And I am getting collection of PermissionGroupDTO which should contains collection of GroupActionPermissionDTO, but that collection stays null. Is there something wrong with my code? I am afraid that automapper can map collections from foreign keys.
Also, thats my automapper initializer:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PermissionGroup, PermissionGroupDTO>();
cfg.CreateMap<GroupActionPermission, GroupActionPermissionDTO>();
});
I believe the reason is desribed here http://docs.automapper.org/en/stable/Queryable-Extensions.html
Note that for this feature to work, all type conversions must be explicitly handled in your Mapping.
So that means you should manually configure the mapping:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PermissionGroup, PermissionGroupDTO>()
.ForMember(dto => dto.ActionPermissions , conf => conf.MapFrom(ol => ol.GroupActionPermissions )));;
cfg.CreateMap<GroupActionPermission, GroupActionPermissionDTO>();
});
BTW, note that fields are named differently: GroupActionPermissions vs. ActionPermissions. This is also the reason why automapper doesn't map it automatically and then you should use the manual configuration I wrote.
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; }
}
I have the following class
public class Unit
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("unittype")]
public UnitType UnitType { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("managers")]
public List<Manager> Managers { get; set; }
[JsonProperty("logoURL")]
public string LogoURL { get; set; }
[JsonProperty("naceCodeList")
public List<string> NaceCodeList { get; set; }
[JsonProperty("tags")]
public List<string> Tags { get; set; }
[JsonIgnore]
public string JoinedTags => string.Join(",", Tags);
[JsonProperty("respondents")]
public List<Respondent> Respondents { get; set; }
[JsonProperty("childUnits")]
public List<Unit> ChildUnits { get; set; }
public string Parent { get; set; }
}
I also have the following in the constructor of my BaseRepository
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Unit, Unit>();
});
My problem is that when I, in my repository, do Mapper.Map(unitToUpdate, unit); it empties all the List<whatever> properties.
Is this default behavior? Am I doing something wrong here?
For completeness, here is my UpdateUnit function
public void UpdateUnit(ClaimsPrincipal user, string orgId, Unit unitToUpdate)
{
var org = Get(user, orgId);
var unit = org.GetUnit(unitToUpdate.Id);
Mapper.Map(unitToUpdate, unit);
Update(user, org);
}
The stored object is another type containing a hierarchy of Unit objects representing a company. The org object is what is stored in the database, and the class representing the org object has it's own functions to load Unit objects inside itself.
I guess that your problem is that you need first to define the mappings for all collections from both objects and after that you need to define the mappings for objects.
Idea is to get from the bottom to the top (from the nested one to the Unit).
For example (taking into consideration only Respondents:
Mapper.CreateMap<RespondentDb, RespondentDto>();
Mapper.CreateMap<UnitDb, UnitDto>()
.ForMember(dest => dest.Respondents, opt => opt.MapFrom(src => src.Respondents);
Some references:
https://github.com/AutoMapper/AutoMapper/wiki/Nested-mappings
https://codereview.stackexchange.com/questions/70856/automapper-mapping-a-nested-collection-to-a-list (here we have an object that only contains a list, maybe it's good to know that too as you can encounter the same scenario)
I've got a source model defined as
public class SourceRoot
{
...
public Organisation Organisation { get; set; }
...
}
public class Organisation
{
public long? Id { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public double SupplementaryAmount { get; set; }
public decimal BaseConversionRate { get; set; }
}
and a destination defined as:
public class DestinationRoot
{
...
public Organisation Organisation { get; set; }
public ContributesTo ContributesTo { get; set; }
}
public class Organisation
{
public long? Id { get; set; }
public string Name { get; set; }
}
public class ContributesTo
{
public string Currency { get; set; }
public double SupplementaryAmount { get; set; }
public decimal BaseConversionRate { get; set; }
}
I want to map from the SourceRoot to the DestinationRoot add copy from the source Organisation to the destination Organisation AND ContributesTo.
I have the following configuration for AutoMapper:
public static class AutoMapperConfig
{
public static MapperConfiguration RegisterMappings()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<MyProfile>();
});
return config;
}
}
public class MyProfile : Profile
{
protected override void Configure()
{
this.CreateMap<SourceRoot, DestinationRoot>();
this.CreateMap<Source.Organisation, Destination.Organisation>();
this.CreateMap<Source.Organisation, Destination.ContributesTo>();
}
}
Using this current profile the Organisation gets mapped but the ContributesTo comes out as null.
Note that I'm using version 4.2 of AutoMapper where the static methods have been deprecated so trying to steer away from that. Normally I would do:
Mapper.CreateMap<SourceRoot, DestinationRoot>()
.ForMember(d => d.ContributesTo, opt => opt.MapFrom( s=> Mapper.Map<ContributesTo>(s.Organisation)));
But this is not advised anymore (referencing the static methods). Is there an alternative way of doing this?
Thanks
Just add mapping for ContributesTo destination member:
protected override void Configure ()
{
CreateMap<Source.SourceRoot, Destination.DestinationRoot>()
.ForMember(d => d.ContributesTo, opt => opt.MapFrom(s => s.Organisation));
CreateMap<Source.Organisation, Destination.Organisation>();
CreateMap<Source.Organisation, Destination.ContributesTo>();
}
Otherwise Automapper finds that both source and destinaton roots have property Organisation and it maps only this property. Automapper cannot understand that it should use one property of source to map several properties of destination (which do not match by name). Note that you don't need to specify mapping for Organisation member, because it matches property name in destination object.
I have an Asp_Users table and a Resource tables. In the Entity Framework world, I have a Asp_Users POCO and a Resource POCO. Moreover, the Resource POCO is abstract and is part of a Table-per-Hierarchy model. The Table-per-Hierarchy model has the abstract Resource POCO and several Concrete POCOs like ILCResource POCO and SectionResource POCO. There is a one-to-many (1 to 0…*) relationship from Asp_Users POCO (one-side) to Resource POCO (many-side).
Here's the relevant part of my aspnet_Users POCO:
public partial class aspnet_Users
{
public aspnet_Users() { }
public virtual System.Guid ApplicationId { get; set; }
public virtual System.Guid UserId { get; set; }
public virtual string UserName { get; set; }
public virtual string LoweredUserName { get; set; }
public virtual string MobileAlias { get; set; }
public virtual bool IsAnonymous { get; set; }
public virtual System.DateTime LastActivityDate { get; set; }
public virtual ICollection<Resource> associatedResources { get; set; }
}
Here is my mapping configuration for Asp_Users
public class Aspnet_UsersMap : EntityTypeConfiguration<PerlsData.Domain.aspnet_Users>
{
public Aspnet_UsersMap()
{
this.ToTable("aspnet_Users", schemaName: "dbo");
this.HasKey(u => u.UserId);
this.Property(u => u.UserId)
.HasColumnName("UserId")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.HasOptional(u => u.associatedResources);
}
}
Here's the relevant part of my abstract Resource POCO class:
public abstract class Resource
{
public Resource(){
// associatedPerspectives = new HashSet<Perspective>();
}
public virtual System.Guid ResourceDatabaseID { get; set; }
public virtual string ResourceName { get; set; }
public virtual string DescriptionOfResource { get; set; }
public virtual System.Guid UserId { get; set; }
public virtual Nullable<System.Guid> DepartmentDatabaseID { get; set; }
public virtual string ResourceStatus { get; set; }
public virtual Nullable<short> isRemoved { get; set; }
public virtual Department Department { get; set; }
public virtual System.Guid UserId { get; set; }
public virtual aspnet_Users aspnet_Users { get; set; }
public virtual ICollection<ResourceOverToILCResourcesBridge> associatedResourceOverToILCResourcesBridgeEntry { get; set; }
}
Here is my mapping configuration for Resource:
public class ResourceMap : EntityTypeConfiguration<Resource>
{
public ResourceMap()
{
this.ToTable("Resources", schemaName: "dbo");
this.Property(r => r.ResourceDatabaseID)
.HasColumnName("ResourceDatabaseID");
this.HasKey(r => r.ResourceDatabaseID);
this.Property(x => x.ResourceDatabaseID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// .StoreGeneratedPattern = StoreGeneratedPattern.Identity;
this.Property(r => r.ResourceName)
.HasColumnName("ResourceName");
this.Map<PerlsData.Domain.OtherItem>(m => m.Requires("discriminator").HasValue("otheritems"))
.Map<PerlsData.Domain.Audioitem>(m => m.Requires("discriminator").HasValue("audioitems"))
.Map<PerlsData.Domain.Imageitem>(m => m.Requires("discriminator").HasValue("imageitems"))
.Map<PerlsData.Domain.Videoitem>(m => m.Requires("discriminator").HasValue("videoitems"))
.Map<PerlsData.Domain.UriItem>(m => m.Requires("discriminator").HasValue("uriitems"))
.Map<PerlsData.Domain.Documentitem>(m => m.Requires("discriminator").HasValue("documentitems"))
.Map<PerlsData.Domain.DatabaseFileItem>(m => m.Requires("discriminator").HasValue("databasefileitems"));
this.HasOptional(res => res.associatedResourceOverToILCResourcesBridgeEntry);
this.HasRequired(res => res.aspnet_Users)
.WithMany(u => u.associatedResources)
.HasForeignKey(res => res.UserId);
}
}
Could you please tell me why I am getting the following error?
The navigation property 'associatedResources' declared on type
'PerlsData.Domain.aspnet_Users' has been configured with conflicting
multiplicities.
Please Explain Why it's still NULL after I created the mapping in the POCO class.
this.HasOptional(u => u.associatedResources);
That's wrong. HasOptional means 0..1.
You want HasMany().
In fact, you can get rid of that line entirely; EF can figure it out from the property.
I need to study more about the IQueryable and/or IEnumerable collections along with the lambda expression queries that are used to retrieve data, and place them in the aforementioned IQueryable and/or IEnumerable.
My problem was that I was trying to retrieve the associations of a particular POCO within a loop that iterates over the IQueryable and/or IEnumerable collections.
Here is the Faulty code (we are accessing association while we iterate over IQueryable):
resourcestempIQueryable = context.Resources;
foreach (PerlsData.Domain.Resource tempRes in resourcestempIQueryable)
{
string[] resourceRow = { tempRes.ResourceName,
tempRes.DescriptionOfResource,
tempRes.UploadDate.ToString(),
tempRes.Iconpath,
tempRes.aspnet_Users.UserName, // Error
tempRes.ResourceDatabaseID.ToString() };
tempDataTableForResources.Rows.Add(resourceRow);
}
Here is the proper code that should resolve the issue:
using (PerlsData.Context context = new PerlsData.Context())
{
context.Configuration.LazyLoadingEnabled = true;
context.Configuration.ProxyCreationEnabled = true;
resourcesIEnumerable = context.Resources.ToList<PerlsData.Domain.Resource>();
var entityValuesWithFieldsToShowInAllResourcesPage = context.Resources.Select(res => new { res.ResourceName, res.DescriptionOfResource, res.UploadDate, res.Iconpath, res.aspnet_Users.UserName, res.ResourceDatabaseID });
foreach (var varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage in entityValuesWithFieldsToShowInAllResourcesPage)
{
string[] resourceRow = {
varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.ResourceName,
varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.DescriptionOfResource,
varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.UploadDate.ToString(),
varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.Iconpath,
varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.UserName,
varForIteratingOverEntityValuesWithFieldsToShowInAllResourcesPage.ResourceDatabaseID.ToString()
};
tempDataTableForResources.Rows.Add(resourceRow);
}
}