Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to query a table and map all the related tables using FromSqlRaw() in EF Core?

How to query a table and map all the related tables using FromSqlRaw() in EF Core?

Scheduled Pinned Locked Moved C#
jsonasp-netdatabasetutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Wright 2022
    wrote on last edited by
    #1

    I want to write a query for the API to get all desired data as JSON while keeping all related tables in the final result. I have the following model: Equipment.cs

    public class Equipment
    {
    [Key]
    public long EquipmentId { get; set; }
    [Required]
    public string EquipmentCode { get; set; }
    [Required]
    public string EquipmentTitle { get; set; }
    [Required]
    public bool IsActive { get; set; }

    //Navigation properties
    \[Required\]
    public int CostCenterId { get; set; }
    public CostCenter CostCenter { get; set; }
    public int? EquipmentCategoryId { get; set; }
    public EquipmentCategory? EquipmentCategory { get; set; }
    public int EquipmentTypeId { get; set; }
    public EquipmentType equipmentType { get; set; }
    

    }

    CostCenter.cs

    public class CostCenter
    {
    [Key]
    public int CostCenterId { get; set; }
    [Required]
    [MaxLength(100)]
    public string Title { get; set; }
    [Required]
    public bool IsActive { get; set; } = true;

    //Navigation properties
    \[JsonIgnore\]
    public virtual List? Equipments { get; set; }
    

    }

    EquipmentCategory.cs

    public class EquipmentCategory
    {
    [Key]
    public int EquipmentCategoryId { get; set; }
    [Required]
    [MaxLength(100)]
    public string CategoryCode { get; set; }
    [Required]
    [MaxLength(100)]
    public string CategoryName { get; set; }

    //Navigation property
    \[JsonIgnore\]
    public virtual List? Equipments { get; set; }
    

    }

    EquipmentType.cs

    public class EquipmentType
    {
    [Key]
    public int EquipmentTypeId { get; set; }
    [Required]
    [MaxLength(100)]
    public string EquipmentTypeTitle { get; set; }

    //Navigation property
    \[JsonIgnore\]
    public virtual List? Equipments { get; set; }
    

    }

    I use the following codes in the controller:

    var query = "SELECT * FROM Equipments WHERE EquipmentCode LIKE '1101%'"

    var result = _sqlServerContext.Equipments.FromSqlRaw(query);

    return Ok(result);

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups