Patrick, thanks for advice about correct question!
EDIT 1:
I have three table for many to many relationship. Like this:
GoodEntity:
public partial class GoodEntity
{
public GoodEntity()
{
this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>();
}
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal cost { get; set; }
public Nullable<decimal> price { get; set; }
public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; }
}
ProviderEntity:
public partial class ProviderEntity
{
public ProviderEntity()
{
this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>();
}
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string address { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string url { get; set; }
public Nullable<int> rating { get; set; }
public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; }
}
Entity for many-to-many relationship:
public partial class GoodAndProviderEntity
{
public int id { get; set; }
public int good_id { get; set; }
public int provider_id { get; set; }
public virtual GoodEntity Goods { get; set; }
public virtual ProviderEntity Providers { get; set; }
}
GoodDTO:
public class GoodDTO
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal cost { get; set; }
public decimal? price { get; set; }
public IList<ProviderDTO> providers { get; set; }
}
ProviderDTO:
public class ProviderDTO
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string address { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string url { get; set; }
public int? rating { get; set; }
}
This is code for creation maps:
Mapper.CreateMap<ProviderDTO, ProviderEntity>();
Mapper.CreateMap<ProviderEntity, ProviderDTO>();
Mapper.CreateMap<GoodEntity, GoodDTO>()
.ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));
Mapper.CreateMap<GoodAndProviderEntity, ProviderDTO>();
And it works half. Automapper was mapped "goods" completely and was created list for all providers for this goods. But automapper don`t fill providers.
If I use Mapper.AssertConfigurationIsValid(), then:
Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type ======================================================= ProviderDTO -> ProviderEntity (Destination member list) Core.DTO.ProviderDTO -> DAL.EF.Entities.ProviderEntity (Destination member list) Unmapped properties: GoodsAndProviders ============================================================== GoodAndProviderEntity -> ProviderDTO (Destination member list) DAL.EF.Entities.GoodAndProviderEntity -> Core.DTO.ProviderDTO (Destination member list)
How to create mapping for many-to-many relationship?
Regards, Anton
With your current code you're trying to map the GoodAndProviderEntity into ProviderDTO.
Mapper.CreateMap<GoodEntity, GoodDTO>()
.ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));
What you want to do, is to map ProviderEntity into ProviderDTO, so all you have to do is select the Providers from GoodsAndProviders as a list:
Mapper.CreateMap<GoodEntity, GoodDTO>()
.ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders.Select(y => y.Providers).ToList()));
Related
I have three related Entities in my blazor application Opportunity, AppUser and AssignedOpportunity, What I want to achieve is to map Opportunity and AppUser to a DTO Object ReturnAssignedOpportunityDTO which has similar fields as the entities, using AutoMapper, but am not sure how to do that, below are the entities
public partial class AssignedOpportunity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[ForeignKey("OpportunityID")]
public string OpportunityID { get; set; }
public string Status { get; set; }
public Opportunity opportunity { get; set; }
[ForeignKey("UserID")]
public string UserID { get; set; }
public AppUser User { get; set; }
}
The opportunity
public partial class Opportunity
{
public Opportunity()
{
AssignedOpportunities= new HashSet<AssignedOpportunity>();
}
[Key]
public string ID { get; set; }
public string OpportunityName { get; set; }
public string Description { get; set; }
public string CreatedBy { get; set; }
public DateTime DateCreated { get; set; }
public double EstimatedValue { get; set; }
public string EmployeeNeed { get; set; }
public double RealValue { get; set; }
public string Location { get; set; }
public string ReasonStatus { get; set; }
public string Status { get; set; }
public virtual ICollection<AssignedOpportunity> AssignedOpportunities { get; set; }
}
AppUser Class
public partial class AppUser : IdentityUser
{
public AppUser()
{
AssignedOpportunities = new HashSet<AssignedOpportunity>();
}
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string LGA { get; set; }
public string State { get; set; }
public virtual ICollection<AssignedOpportunity> AssignedOpportunities { get; set; }
}
Here's the DTO Object I want to map to.
public class ReturnOpportunitiesDTO
{
public int ID { get; set; }
public string OpportunityID { get; set; }
public string OpportunityName { get; set; }
public double EstimatedValue { get; set; }
public string EmployeeNeed { get; set; }
public double RealValue { get; set; }
public string Location { get; set; }
public string UserID { get; set; }
public string UserFullName { get; set; }
public string Status { get; set; }
}
Here is my query to fetch the records
var result = await _context.AssignedOpportunities.Include(o => o.opportunity).
ThenInclude(a => a.User).
Where(a=>a.UserID==UserID.ToString()).ToListAsync();
return result;
This is how i usually setup Map Profile
public AssignArtisanProfile()
{
CreateMap<AssignedOpportunity, ReturnOpportunities>();
}
But since I want to map multiple entities, how do I include the other entity
Your scenario is just another example of flattening a complex object. You have properties in child objects, which you want to bring to the ground level, while still leveraging AutoMapper mapping capabilities. If only you could reuse other maps from app user and opportunity when mapping from assigned opportunity to the DTO... Well, there is a method called IncludeMembers() (see the docs) that exists precisely for such case. It allows you to reuse the configuration in the existing maps for the child types:
config.CreateMap<AssignedOpportunity, ReturnOpportunitiesDTO>()
.IncludeMembers(source => source.opportunity, source => source.User);
config.CreateMap<Opportunity, ReturnOpportunitiesDTO>();
config.CreateMap<AppUser, ReturnOpportunitiesDTO>()
.ForMember(
dest => dest.UserFullName,
options => options.MapFrom(source =>
string.Join(
" ",
source.FirstName,
source.MiddleName,
source.LastName)));
Usage:
var mappedDtos = mapper.Map<List<ReturnOpportunitiesDTO>>(assignedOpportuniesFromDatabase);
I am having setup similar to below
public class Component
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public List<ComponentUpdate> Updates { get; set; }
public ComponentUpdate LastUpdate { get; set; }
}
public class ComponentUpdate
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public Component Component { get; set; }
public string Message { get; set; }
}
and I have setup like
modelBuilder.Entity<Component>().HasMany(c => c.Updates).WithOne(u => u.Component);
modelBuilder.Entity<ComponentUpdate>().HasOne(u => u.Component).WithOne(c => c.LastUpdate);
But Entity Framework is throwing an error:
Navigation properties can only participate in a single relationship
What is the problem with above setup? I want to fetch last update at times using navigational property. It seems like an EF limitation...
You must define a separate navigation property for each relationship and you must also define a foreign key for the second relationship:
public class Component
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public List<ComponentUpdate> Updates { get; set; }
public ComponentUpdate LastUpdate { get; set; }
}
public class ComponentUpdate
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public Component Component { get; set; }
public int SecondComponentId { get; set; }
public Component SecondComponent { get; set; }
public string Message { get; set; }
}
builder.Entity<Component>()
.HasMany(c => c.Updates)
.WithOne(u => u.Component);
builder.Entity<ComponentUpdate>()
.HasOne(u => u.SecondComponent)
.WithOne(c => c.LastUpdate)
.HasForeignKey<ComponentUpdate>(x => x.SecondComponentId);
I get the following error:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type.
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
========================================================================
List1 -> PSS_MembersViewModel (Destination member list)
System.Collections.Generic.List`1[[PRS.Domain.Entities.PSS_Members,
PRS.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] ->
PRS_MD.ViewModels.PSS_MembersViewModel (Destination member list)
Unmapped properties: ID Full_Name Mobile Mobile2 Email PSS_Team_ID
PSS_Teams PSS_Trip_Members Active
My entities:
public class PSS_Members : Entity
{
public PSS_Members()
{
PSS_Trip_Members = new HashSet<PSS_Trip_Members>();
}
[StringLength(100)]
public string Full_Name { get; set; }
[StringLength(50)]
public string Mobile { get; set; }
[StringLength(50)]
public string Mobile2 { get; set; }
[StringLength(100)]
public string Email { get; set; }
public int? PSS_Team_ID { get; set; }
public virtual PSS_Teams PSS_Teams { get; set; }
public virtual ICollection<PSS_Trip_Members> PSS_Trip_Members { get; set; }
public bool Active { get; set; }
}
public class PSS_Teams : Entity
{
[StringLength(50)]
public string Description { get; set; }
public virtual ICollection<PSS_Members> PSS_Members { get; set; }
public virtual ICollection<PSS_Team_Support> PSS_Team_Support { get; set; }
public virtual ICollection<PSS_Vehicles> PSS_Vehicles { get; set; }
public bool Active { get; set; }
}
View Models:
public class PSS_MembersViewModel
{
public int ID { get; set; }
[StringLength(100)]
public string Full_Name { get; set; }
[StringLength(50)]
public string Mobile { get; set; }
[StringLength(50)]
public string Mobile2 { get; set; }
[StringLength(100)]
public string Email { get; set; }
public int? PSS_Team_ID { get; set; }
// public virtual PSS_Teams PSS_Teams { get; set; }
public virtual PSS_TeamsViewModel PSS_Teams { get; set; }
public virtual ICollection<PSS_Trip_Members> PSS_Trip_Members { get; set; }
public bool Active { get; set; }
}
public class PSS_TeamsViewModel
{
public int ID { get; set; }
public string Description { get; set; }
public virtual ICollection<PSS_MembersViewModel> PSS_Members { get; set; }
}
Mapper:
cfg.CreateMap<PSS_Members, PSS_MembersViewModel>()
.ReverseMap();
cfg.CreateMap<PSS_Teams, PSS_TeamsViewModel>()
.ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.ID))
.ReverseMap();
Controller:
var members = _pSS_MembersService.GetAll().ToList();
var model = AutoMapper.Mapper.Map<PSS_MembersViewModel>(members);
var teams = _pSS_TeamsService.GetAll().ToList();
var mappedteams = AutoMapper.Mapper.Map<PSS_TeamsViewModel>(teams);
model.PSS_Teams = mappedteams;
You are trying to map a list of Entities to a single View Model.
// var mappedteams = AutoMapper.Mapper.Map<PSS_TeamsViewModel>(teams);
var mappedteams = AutoMapper.Mapper.Map<List<PSS_TeamsViewModel>>(teams);
I have two classes:
One is User
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<Subscription> Subscriptions { get; set; }
}
Other is Subscription:
public class Subscription
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
As you can see that User has a list of Subscriptions.
Now when using the entity framework code first approach I am getting a table for User which doesn't contain Subscriptions but a new column for User Id is being added to Subscription table. I was expecting to have a third table which contains two columns one with User ID and the other with subscription ID.
How can I achieve this?
From documentation:
Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.
So this answer is correct.
I just corrected code a little bit:
class MyContext : DbContext
{
public DbSet<Use> Users { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserSubscription>()
.HasKey(t => new { t.UserId, t.SubscriptionId });
modelBuilder.Entity<UserSubscription>()
.HasOne(pt => pt.User)
.WithMany(p => p.UserSubscription)
.HasForeignKey(pt => pt.UserId);
modelBuilder.Entity<UserSubscription>()
.HasOne(pt => pt.Subscription)
.WithMany(t => t.UserSubscription)
.HasForeignKey(pt => pt.SubscriptionId);
}
}
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<UserSubscription> UserSubscriptions{ get; set; }
}
public class Subscription
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public List<UserSubscription> UserSubscriptions{ get; set; }
}
public class UserSubscription
{
public int UserId { get; set; }
public User User { get; set; }
public int SubscriptionId { get; set; }
public Subscription Subscription { get; set; }
}
PS. You don't need use virtual in navigation property, because lazy loading still not available in EF Core.
Create a third middle table named: UserSubscriptions for example.
public class User
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public virtual ICollection<UserSubscription> Subscriptions { get; set; }
}
public class Subscription
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class UserSubscription
{
public int ID { get; set; }
public int SubscriptionID { get; set; }
public decimal Price { get; set; }
public int UserID { get; set; }
public virtual User { get; set; }
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
}
Second Solution:
Add reference for Subscription to User and name it CurrentSubscription for example.
public class User
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int CurrentSubscriptionID { get; set; }
public virtual Subscription Subscription { get; set; }
}
public class Subscription
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
I am working on a legacy database that has 2 tables that have a 1:1 relationship.
Currently, I have one type (1Test:1Result) for each of these tables defined
I would like to merge these particular tables into a single class.
The current types look like this
public class Result
{
public string Id { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public string Units { get; set; }
public bool OutOfRange { get; set; }
public string Status { get; set; }
public string Minimum { get; set; }
public string Maximum { get; set; }
public virtual Instrument InstrumentUsed { get; set; }
public virtual Test ForTest { get; set; }
}
public class Test
{
public int Id { get; set; }
public string Status { get; set; }
public string Analysis { get; set; }
public string ComponentList { get; set; }
public virtual Sample ForSample { get; set; }
public virtual Result TestResult { get; set; }
}
I would prefer them to look like this
public class TestResult
{
public int Id { get; set; }
public string Status { get; set; }
public string Analysis { get; set; }
public string ComponentList { get; set; }
public string TestName { get; set; }
public string Text { get; set; }
public string Units { get; set; }
public bool OutOfRange { get; set; }
public string Status { get; set; }
public string Minimum { get; set; }
public string Maximum { get; set; }
public virtual Instrument InstrumentUsed { get; set; }
}
I am currently using the fluent API for mapping these to our legacy Oracle database.
What would be the best method of combining these into a single class?
Please note that this is a legacy database. Changing the tables is not an option and creating views is not a viable solution at this point in the project.
You can use Entity Splitting to achieve this if you have the same primary key in both tables.
modelBuilder.Entity<TestResult>()
.Map(m =>
{
m.Properties(t => new { t.Name, t.Text, t.Units /*other props*/ });
m.ToTable("Result");
})
.Map(m =>
{
m.Properties(t => new { t.Status, t.Analysis /*other props*/});
m.ToTable("Test");
});
Here's a useful article