自动映射和关系数据库

时间:2017-09-13 10:09:17

标签: c# 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可以有一个Level4)

我正在使用Entity Framework。它生成了类:

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

现在我需要将Level4表添加到映射中。你能帮忙吗?

- 编辑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 =&lt; tblSystemApplications&gt; = o tblSystems 我想在ApplicationDto中查看System。

- 编辑2

添加了EF生成的类并更新了ApplicationDto类

1 个答案:

答案 0 :(得分:3)

如果您的(生成的数据库)模型如下所示:

def data_pdf_base64
  begin
    # Create Prawn Object
    my_pdf = Prawn::Document.new
    # write text to pdf
    my_pdf.text("Hello Gagan, How are you?")
    # Save at tmp folder as pdf file
    my_pdf.render_file("#{Rails.root}/tmp/pdf/gagan.pdf")
    # Read pdf file and encode to Base64
    encoded_string = Base64.encode64(File.open("#{Rails.root}/tmp/pdf/gagan.pdf"){|i| i.read})
    # Delete generated pdf file from tmp folder
    File.delete("#{Rails.root}/tmp/pdf/gagan.pdf") if File.exist?("#{Rails.root}/tmp/pdf/gagan.pdf")
    # Now converting Base64 to pdf again
    pdf = Prawn::Document.new
    # I have used ttf font because it was giving me below error
    # Your document includes text that's not compatible with the Windows-1252 character set. If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts.
    pdf.font Rails.root.join("app/assets/fonts/fontawesome-webfont.ttf")
    pdf.text Base64.decode64 encoded_string
    pdf.render_file("#{Rails.root}/tmp/pdf/gagan2.pdf")
  rescue => e
    return render :text => "Error: #{e}"
  end
end

然后这种映射是有效的:

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

如果您的型号不同,请与我们联系。

- 编辑1
您的多对多案例应该实现如下:

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

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; } } 中考虑您有复合/复合主键tblSystemApplicationApplicationID

您将在SystemID中获得tblSystem的列表,然后将其映射到您想要的任何内容。