2017-09-13 73 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