2017-09-13 105 views
1

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

+0

https://stackoverflow.com/questions/21413273/automapper-convert-from-multiple-sources – CodeCaster

+0

如果您的應用程序中包含很多系統,如果您在您的tbl中使用EF實體你有一個tblkSystems的列表。 – Soren

+0

@Soren你能說清楚你的意思嗎?如果您需要,我可以分享更多細節。 – ironcurtain

回答

3

如果你的(生成的數據庫)模型是這樣的:

public class tblApplications 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public tblLevel5s tblLevel5 { get; set; } 

} 
public class tblLevel5s 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public tblLevel4s tblLevel4 { get; set; } 
} 
public class tblLevel4s 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

那麼這種映射的是有效的:

var config = new MapperConfiguration(cfg => { 
    cfg.CreateMap<tblApplications, ApplicationDto>() 
    .ForMember(dest => dest.L5, opt => opt.MapFrom(src => src.tblLevel5)) 
    .ForMember(dest => dest.L4, opt => opt.MapFrom(src => src.tblLevel5.tblLevel4));     
}); 

如果您的型號不同,請讓我知道。


- 編輯1
你多到許多情況下,應實行這樣的事:

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<tblSystem> tblSystems { get; set; } 
} 

public partial class tblSystemApplication 
{ 
    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<tblApplication> tblApplications { get; set; } 
} 

考慮,在tblSystemApplication你有複合/複合主鍵ApplicationIDSystemID

你會在你的tblApplication中得到一個tblSystem的列表你可以將它映射到任何你想要的。

+0

謝謝。這是我正在尋找的。我知道這很簡單,但我無法自己解決「MapFrom(src => src.tblInventoryLevel5s.tblInventoryLevel4s)」部分。 – ironcurtain