Reputation: 850
I have 2 tables that saved family members and the below LINQ is 100% work and got result return. I tried to map using Automapper but it's does not work , the customerViewItem
does not have data and no error , could some one please advise, thanks in advance.
POCO
public class Cust_ProfileTbl
{
[Key]
public long bintAccountNo { get; set; }
public string nvarCardName { get; set; }
public string varEmail { get; set; }
public virtual ICollection<Cust_ProfileFamilyTbl> profileFamilyParents { get; set; }
public virtual ICollection<Cust_ProfileFamilyTbl> profileFamilyChildren { get; set; }
}
public class Cust_ProfileFamilyTbl
{
[Key]
public int intProfileFamily { get; set; }
public long bintAccountNo { get; set; }
public long bintAccountNoMember { get; set; }
public virtual Cust_ProfileTbl custProfileParent { get; set; }
public virtual Cust_ProfileTbl custProfileChild { get; set; }
}
In onModelCreating
modelBuilder.Entity<Cust_ProfileFamilyTbl>()
.HasRequired(m => m.custProfileParent)
.WithMany(t => t.profileFamilyParents)
.HasForeignKey(m => m.bintAccountNo)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Cust_ProfileFamilyTbl>()
.HasRequired(m => m.custProfileChild)
.WithMany(t => t.profileFamilyChildren)
.HasForeignKey(m => m.bintAccountNoMember)
.WillCascadeOnDelete(false);
ViewModels
public class Profile
{
public long bintAccountNo { get; set; }
public string varCardNo { get; set; }
public string nvarCardName { get; set; }
public string varEmail { get; set; }
public virtual ICollection<ProfileFamily> profileFamilyParents { get; set; }
public virtual ICollection<ProfileFamily> profileFamilyChildren { get; set; }
public Profile()
{
profileFamilyParents = new Collection<ProfileFamily>();
profileFamilyChildren = new Collection<ProfileFamily>();
}
}
public class ProfileFamily
{
public int intProfileFamily { get; set; }
public long bintAccountNo { get; set; }
public long bintAccountNoMember { get; set; }
public Profile custProfileParent { get; set; }
}
LINQ and AutoMapper
System.Linq.Expressions.Expression<Func<Cust_ProfileTbl, bool>> wherep = (x) => x.bintAccountNo.Equals(1);
Cust_ProfileTbl rs = (from family in context.member.Include("profileFamilyParents.custProfileChild")
.Where(wherep)
select family).Single();
Mapper.CreateMap<Cust_ProfileTbl, EFWeb.ViewModels.Profile>();
EFWeb.ViewModels.Profile customerViewItem = Mapper.Map<Cust_ProfileTbl, EFWeb.ViewModels.Profile>(rs);
Upvotes: 0
Views: 10370
Reputation: 574
First thing is you should not create a map for mapping a List<A>
to a List<B>
. You should just create a map from A
to B
--automapper knows how to map a List<A>
to a List<B>
if you give it a map from A
to B
. This means your map should be:
Mapper.CreateMap<Cust_ProfileTbl, EFWeb.ViewModels.Profile>();
Second, automapper will not know how to map your ICollection<Cust_ProfileFamilyTbl>
properties in your entity to the ICollection<ProfileFamily>
properties in your viewmodel. You need to provide a map for those types:
Mapper.CreateMap<Cust_ProfileFamilyTbl, EFWeb.ViewModels.ProfileFamily>();
Upvotes: 2
Reputation: 33815
It is always bet to initialize your maps inside of the startup entry point of the app. In an MVC project this would be in the global.asax and in a WPF application it would be the app.xaml.cs file.
If we were initializing in the global.asax, it would look something like this:
protected void Application_Startup()
{
Mapper.CreateMap<Cust_ProfileTbl, EFWeb.ViewModels.Profile>();
}
Notice we're not mapping from type to type, not from list of type to list of type, meaning we need to iteratively map.
List<EFWeb.ViewModels.Profile> customerViewItem = rs.Select(x => Mapper.Map<Cust_ProfileTbl>(x)).ToList();
Now, there's a good chance that your map will still fail, since your properties do not match on each side. If this is the case, you should use .IgnoreMember() extension methods on your CreateMap to ignore the members that cannot be mapped because they have no corresponding receiving type.
Upvotes: 1