我有三个表:Automapper和关系数据库
tblApplications
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](250) NOT NULL,
[Level5ID] [int] NOT NULL
tblLevel5s
[ID] [int] IDENTITY(1,1) NOT NULL,
[Level4ID] [int] NOT NULL,
[Name] [varchar](250) NOT NULL
tblLevel4s
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](250) NOT NULL
表之间的关系是: tblApplications> = O tblLevel5s> = O tblLevel4s (应用程序可以有一个LEVEL5和LEVEL5可以具有一个级别4)
我正在使用实体框架。它生成的类:
public partial class tblApplication
public int ID { get; set; }
public string Name { get; set; }
public int Level5ID { get; set; }
public virtual tblLevel5s tblLevel5s { get; set; }
public virtual ICollection<tblSystemApplication> tblSystemApplications { get; set; }
}
public partial class tblSystemApplication
{
public int ID { get; set; }
public int ApplicationID { get; set; }
public int SystemID { get; set; }
public virtual tblApplication tblApplication { get; set; }
public virtual tblSystem tblSystem { get; set; }
}
public partial class tblSystem
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<tblSystemApplication> tblSystemApplications { get; set; }
}
我的目标是,利用automapper,创建一个类,它看起来像:
public class ApplicationDto
{
public int Id { get; set; }
public SystemDto System { get; set; }
public string Name { get; set; }
public Level5Dto Level5 { get; set; }
public Level4Dto Level4 { get; set; }
public IEnumerable<SystemAppliationDto> SystemApplications { get; set; }
}
我不知道我是否正确设计我的ApplicationDto类。请告知是否应该更改任何内容。
我至今是:
cfg.CreateMap<tblApplications, ApplicationDto>()
.ForMember(dest => dest.Level5, opt => opt.MapFrom(src => src.tblLevel5s))
现在我需要级别4表添加到映射。你能帮忙吗?
- 编辑1
什么情况下我有多对多的关系?我有中间表像
tblApplications
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](250) NOT NULL
tblSystems
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](250) NOT NULL
tblSystemApplications
[ID] [int] IDENTITY(1,1) NOT NULL,
[ApplicationID] [int] NOT NULL,
[SystemID] [int] NOT NULL
关系是tblApplications O = < tblSystemApplications> = O tblSystems 我想在ApplicationDto视图来获取系统。
- EDIT 2
新增EF生成的类和更新ApplicationDto类
https://stackoverflow.com/questions/21413273/automapper-convert-from-multiple-sources – CodeCaster
如果您的应用程序中包含很多系统,如果您在您的tbl中使用EF实体你有一个tblkSystems的列表。 – Soren
@Soren你能说清楚你的意思吗?如果您需要,我可以分享更多细节。 – ironcurtain