自动装置不工作

本文关键字:工作 自动装置 | 更新日期: 2023-09-27 18:11:27

我有2个表,保存了家庭成员和下面的LINQ是100%的工作,并得到了结果返回。我尝试使用Automapper绘制地图,但它不起作用,customerViewItem没有数据也没有错误,请有人建议,提前感谢。

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; }
}
在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);

视图模型

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);

自动装置不工作

首先,您不应该创建映射将List<A>映射到List<B>的映射。你应该创建一个从AB的映射——如果你给它一个从AB的映射,automapper知道如何将List<A>映射到List<B>。这意味着你的映射应该是:

Mapper.CreateMap<Cust_ProfileTbl, EFWeb.ViewModels.Profile>();

第二,automapper不知道如何将实体中的ICollection<Cust_ProfileFamilyTbl>属性映射到视图模型中的ICollection<ProfileFamily>属性。您需要为这些类型提供一个映射:

Mapper.CreateMap<Cust_ProfileFamilyTbl, EFWeb.ViewModels.ProfileFamily>();

最好在应用程序的启动入口点内初始化你的地图。在MVC项目中,这将是在全局。在WPF应用程序中,它将是app.xaml.cs文件。

如果在全局变量中初始化。现在,它看起来像这样:

protected void Application_Startup()
{
    Mapper.CreateMap<Cust_ProfileTbl, EFWeb.ViewModels.Profile>();
}

注意,我们不是从类型映射到类型,也不是从类型的列表映射到类型的列表,这意味着我们需要迭代映射。

List<EFWeb.ViewModels.Profile> customerViewItem = rs.Select(x => Mapper.Map<Cust_ProfileTbl>(x)).ToList();

现在,你的地图很有可能仍然会失败,因为你的属性在每一边都不匹配。如果是这种情况,你应该在你的CreateMap上使用。ignoremember()扩展方法来忽略那些不能被映射的成员,因为它们没有相应的接收类型。