title: Asp .net core WebApi to return Foreign key Id as value
body: <p>net 
  Here I'm working on Asp .net core using Entity Framework Core(version 3.1.2) and i'm using <strong><em>mssql</em></strong> server as my DB</p>
  
  <p><strong>i have two entity class Region and country</strong></p>
  
  <p><strong>Region.cs</strong></p>
  
  <pre><code>[Key]
      public int Id { get; set; }
      [Column(TypeName ="nvarchar(20)")]
      [Required]
      public string Code { get; set; }
      [Column(TypeName = "nvarchar(150)")]
      [Required]
      public string Description { get; set; }
      [Column(TypeName = "Bit")]
      [Required]
      public Boolean Active { get; set; }
  </code></pre>
  
  <p><strong>Country.cs</strong></p>
  
  <pre><code>[Key]
  public int Id { get; set; }
  
  [Required]
  public string Code { get; set; }
  [Column(TypeName = "nvarchar(150)")]
  [Required]
  public string Description { get; set; }
  [Column(TypeName = "Bit")]
  [Required]
  public Boolean Active { get; set; }
  [Required]
  public int RegionId { get; set; }
  [ForeignKey("RegionId")]
  public virtual Region Region { get; set;}
  </code></pre>
  
  <p>And my <strong>Db context</strong></p>
  
  <pre><code>    public class MasterContext: DbContext
    {
      public MasterContext(DbContextOptions options):  base(options)
      {}
      public DbSet&lt;Region&gt; regions { get; set; }
      public DbSet&lt;country&gt; countries { get; set; }
    }
  </code></pre>
  
  <p>and my <strong>startup.cs</strong></p>
  
  <pre><code>services.AddMvc()
             .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
             .AddNewtonsoftJson(options =&gt; {
               var resolver = options.SerializerSettings.ContractResolver;
               if (resolver != null)
                 (resolver as DefaultContractResolver).NamingStrategy = null;
             });
    services.AddControllers();
    services.AddDbContext&lt;MasterContext&gt;(options =&gt;
    options.UseSqlServer(Configuration.GetConnectionString("dev")));
  
      }
  </code></pre>
  
  <p><strong>connection String</strong> in <em>appsettings.json</em></p>
  
  <pre><code>**"ConnectionStrings": {
  "dev": "Server=ADMIN;Database=MASTERS;User Id=vibe;Password=vibe"
  </code></pre>
  
  <p>}</p>
  
  <p><strong>country controller</strong></p>
  
  <pre><code>// GET: api/countries/5
          [HttpGet("{id}")]
          public async Task&lt;ActionResult&lt;country&gt;&gt; Getcountry(int id)
          {
              var country = await _context.countries.Include(i =&gt; i.Region).FirstOrDefaultAsync(i =&gt; i.Id == id);
  
              if (country == null)
              {
                  return NotFound();
              }
  
              return country;
          }
  </code></pre>
  
  <p>when i get the value by passing the Url(<a href="http://localhost:65177/api/regions/10" rel="nofollow noreferrer">http://localhost:65177/api/regions/10</a>) in the postman tool
  it <em>returns</em> value like this</p>
  
  <p><strong>what i get</strong></p>
  
  <pre><code>    {
      "Id":10,
      "Code":"inn",
      "Description":"india",
      "Active":true,
      "RegionId":5,
      "Region": {
      "Id":5,
      "Code":"me",
      "Description":"middle east",
      "Active":true
  }
      }
  </code></pre>
  
  <p>And what i'm expecting is this</p>
  
  <pre><code>{
  "Id":10,
  "Code":"inn",
  "Description":"india",
  "Active":true,
  "RegionId": {
  "Id":5,
  "Code":"me",
  "Description":"middle east",
  "Active":true
  }
  }
  </code></pre>
  
  <p>pls anyone give me a solution to solve this...thanks in advance.</p>