0

I have two tables which are Process and ProcessLinkedProcess. The ProcessLinkedProcess table associate with Process table which links associated processes. Models are as follows: Process Model

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 Model

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

    public Process LinedProcess { get; set; }

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

I created DTOs for both models and create Automapper link too

Automapper

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

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

I want to get both tables data through web api to display as a list in a table. How can I pass both tables data through web api get request. So far I am getting process data as follow

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

Please help.!

PNP
  • 361
  • 5
  • 17
  • 1
    So what seems to be the problem? – CodingYoshi Feb 17 '18 at 07:45
  • isnt adding this.Include("ProcessLinkedProcess") resolves the issue? – G_S Feb 17 '18 at 07:46
  • i need to associate ProcessLinkedProcess with Process. Process model don't have foreign key to processlinkedprocess beside processlinkedprocess has foreign key from process model. Any way to get linked data? – PNP Feb 17 '18 at 09:23
  • @G_S : No its not working. because Process table don't have foreign key from ProcessLinkedProcess table. Its wise-versa. ProcessLinkedProcess table has foreign key from Process table. I want to combine both table data and pass it to jquery.datatable using web api – PNP Feb 17 '18 at 09:33

1 Answers1

0

I just wanted to add foreign key for the same table process. just my approach for solving this issue was wrong.

for others use i write it down here:

In my Process table I added

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

And In module builder I override it by

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

Reference : EntityFramework Same Table Many to Many Relationship

PNP
  • 361
  • 5
  • 17