How to join between two class model Server Type Server Name to get Server Type as Display?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-02-17T15:55:20.4466667+00:00
I work on blazor with .net core 7 . I face issue I can't displaying Server Type Instead of Server Type Id when display all servers Name .

so How to change my GetAll() Function to get Server Type instead of Server Type Id .

With another meaning I need to display Server Id and Server Name from Server Names class with Server Type from Server Type class.

because not good to display Server Type Id for customer or user use application

so I need to use Server Type from Server Type model with ServerId and ServerNames by Joining ServerTypeId from ServerNames Class and ServerType  Class .

on service

public interface IserverNamesService : IRepository<ServerNames>
    {
    }
on controller ServerNames :

private readonly IserverNamesService _IserverNamesService;
        public  ServerNamesController(IserverNamesService IserverName)
        {
        _IserverNamesService = IserverName;
        }
        [HttpGet]
        public IActionResult GetAll()
        {
            return Ok(_IserverNamesService.GetAll());
          
        }
ServerNames Model as below :

public class ServerNames
    {
        [Key]
        public int ServerID { get; set; }
        public string ServerName{ get; set; }
        public int ServerTypeId { get; set; }

    }
Server Type Model as below :

   public class ServerTypes
    {
        [Key]
        public int ServerTypeId  { get; set;}
        public string ServerType { get; set;}
    }
repository used as below :

public interface IRepository<TEntity> where TEntity : class
{
    IEnumerable<TEntity> GetAll();
   
}
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
    internal AppsRepositoryDBContext _context;
    internal DbSet<TEntity> dbSet;

    public BaseRepository(AppsRepositoryDBContext context)
    {
        _context = context;
        this.dbSet = _context.Set<TEntity>();
    }
public IEnumerable<TEntity> GetAll() => _context.Set<TEntity>().ToList();
}
on blazor component ServerNames.razor

<table class="class table-striped">
    <thead>
        <tr>
            <th>Server Id</th>
            <th>Server Name</th>
            <th>Server Type</th>

        </tr>
    </thead>
    <tbody>
        @foreach (var serv in ServerName)
        {
        <tr>
            <td>@serv.serverID</td>
            <td>@serv.serverName</td>
            <td>@serv.serverType</td>
            

        </tr>
        }
    </tbody>

</table>

@code
{
public class ServerNamesClass
    {
        public int serverID { get; set; }
        public string serverName { get; set; }
        public string serverType { get; set; }
    }
    public class ServerTypesClass
    {
        public int serverTypeId { get; set; }
        public string serverType { get; set; }
    }
    private IEnumerable<ServerNamesClass> ServerName = Array.Empty<ServerNamesClass>();
Expected Result as below :

SeverID  ServerName ServerType

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,803 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,665 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 29,946 Reputation points
    2023-02-17T17:21:24.2466667+00:00

    First, it helps if you follow standard framework patterns. Designing related entity models is covered in the following reference documentation.

    Relationships

    EF Core allows three different patterns for loading related data. Loading data covered in the official documentation.

    Loading Related Data

    Unfortunately, you did not show the implementation. Maybe you expect lazy loading which is not turned on by default in EF Core. See the previous link.

    0 comments No comments

  2. Ahmed Salah Abed Elaziz 390 Reputation points
    2023-02-17T18:12:40.8033333+00:00

    can you clear please by code

    0 comments No comments

  3. Zhi Lv - MSFT 33,176 Reputation points Microsoft External Staff
    2023-02-20T03:28:44.11+00:00

    Hi @Ahmed Salah Abed Elaziz

    Welcome to Asp.net core Q&A forum.

    To your issue, you can add a navigation property in the ServerNames model and configure the ServerNames and ServerTypes with One-to-one relationship. After that when query the ServerNames, you can use the Include method to load the related ServerTypes model, after that, you can display the server type based on the navigation property. Code like below:

    Add the navigation property in the ServerNames model:

        public class ServerNames
        {
            [Key]
            public int ServerID { get; set; }
            public string ServerName { get; set; }
            public int ServerTypeId { get; set; }
            [ForeignKey("ServerTypeId")]
            public virtual ServerTypes ServerTypes { get; set; }
        }
    

    Then, in the ServerNamesServices GetAll method, query the database and use Include method to load the ServerNames with ServerTypes:

        public interface IserverNamesService : IRepository<ServerNames>
        { 
        }
    
        public class ServerNamesServices : IserverNamesService
        {
            private readonly TestDbContext _context;
            public ServerNamesServices(TestDbContext testDbContext) {
                _context = testDbContext;
            }
    
            public IEnumerable<ServerNames> GetAll()
            {
                return _context.ServerNames.Include(c=>c.ServerTypes).ToList();
            }   
        }
    

    register the service in the program.cs file, like this:

    builder.Services.AddScoped<IserverNamesService, ServerNamesServices>();
    

    In the Blazor component: in the select statement, create the ServerNamesClass entity based on the query result:

    @page "/serverpage"
    
    <PageTitle>Server Name List</PageTitle>
    
    @using BlazorServerApp.Data
    @using BlazorServerApp.Services
    @inject IserverNamesService servernameService 
     
    @if (servernameclasses == null)
    {
        <p><em>Loading...</em></p>
    }
    else
    {
        <table class="table">
            <thead>
                <tr>
                    <th>Server ID</th>
                    <th>Server Name</th>
                    <th>Server Type</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var servername in servernameclasses)
                {
                    <tr class="draggable"
                data-id="1001"
                id="row1"
                tabindex="1"> 
                        <td>@servername.serverID</td>
                        <td>@servername.serverName</td>
                        <td>@servername.serverType</td>
                    </tr>
                }
            </tbody>
        </table>
    }
     
    @code {
        private List<ServerNamesClass> servernameclasses;
    
        protected override async Task OnInitializedAsync()
        {
            servernameclasses = servernameService.GetAll().Select(c =>
                new ServerNamesClass() { 
                    serverID = c.ServerID, 
                    serverName = c.ServerName, 
                    serverType = c.ServerTypes.ServerType })
                    .ToList();
        }
    }
    

    The database table:

    User's image

    the result as below:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.