我有三個表: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