从web api GET

时间:2018-02-17 07:14:39

标签: c# entity-framework asp.net-mvc-5 automapper

我有两个表ProcessProcessLinkedProcess。与ProcessLinkedProcess表关联的Process表链接关联的进程。型号如下: 过程模型

public class Process
{
    [Key]
    [Display(Name ="Reference")]
    public int ProcessId { get; set; }

    [Required]
    [StringLength(100, ErrorMessageResourceName = "Error_Max_Lenght", ErrorMessageResourceType = typeof(bct.Properties.Resources))]
    [Display(Name = "Clause")]
    public string ProcessClause { get; set; }

    public Function Function { get; set; }

    [Required]
    [Display(Name = "Function")]
    public int FunctionId { get; set; }

    public Employee Owner { get; set; }

    [Required]
    [Display(Name = "Process Owner")]
    public int OwnerEmployeeId { get; set; }

    [Required]
    [StringLength(500, ErrorMessageResourceName = "Error_Max_Lenght", ErrorMessageResourceType = typeof(bct.Properties.Resources))]
    [Display(Name = "Inputs")]
    public string Inputs { get; set; }

    [Required]
    [StringLength(500, ErrorMessageResourceName = "Error_Max_Lenght", ErrorMessageResourceType = typeof(bct.Properties.Resources))]
    [Display(Name = "Outputs")]
    public string Outputs { get; set; }

    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:MM}", ApplyFormatInEditMode = true)]
    public DateTime? ProcessAddedDate { get; set; }

    public UserAccount AddedUser { get; set; }

    public int? AddedUserUserId { get; set; }

    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:MM}", ApplyFormatInEditMode = true)]
    public DateTime? LastUpdatedDate { get; set; }

    public UserAccount LastUpdatedUser { get; set; }

    public int? LastUpdatedUserUserId { get; set; }

}

ProcessLinkedProcess模型

public class ProcessLinkedProcess
{
    [Key]
    public int Id { get; set; }

    public Process LinedProcess { get; set; }

    [Required]
    public int LinedProcessProcessId { get; set; }
}

我为这两个模型创建了DTO并创建了Automapper链接

Automapper

Mapper.CreateMap<Process, ProcessDto>();
Mapper.CreateMap<ProcessLinkedProcess, ProcessLinkedProcessDto>();

Mapper.CreateMap<Process, ProcessDto>()
            .ForMember(p => p.ProcessId, opt => opt.Ignore());

我希望通过web api获取两个表数据,以便在表格中显示为列表。如何通过web api get请求传递两个表数据。到目前为止,我正在获取流程数据,如下所示

public IEnumerable<ProcessDto> GetProcess()
    {
        return _context.Processes
            .Include(a => a.AddedUser)
            .Include(l=>l.LastUpdatedUser)
            .Include(f=>f.Function)                
            .ToList()
            .Select(Mapper.Map<Models.Process, ProcessDto>);
    }

请帮忙。!

1 个答案:

答案 0 :(得分:0)

我只是想为同一个表进程添加外键。只是我解决这个问题的方法是错误的。

对于其他人使用我在这里写下来:

在我的Process表中,我添加了

public virtual ICollection<Process> RelatedProcesses { get; set; }

在模块构建器中,我用

覆盖它
protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

        modelBuilder.Entity<Process>()
            .HasMany(p => p.RelatedProcesses)
            .WithMany()
            .Map(m =>
            {
                m.MapLeftKey("ProcessId");
                m.MapRightKey("RelatedID");
                m.ToTable("Process_Related");
            });
    }

参考:EntityFramework Same Table Many to Many Relationship