public class Employee
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string mobileNumber { get; set; }
public string address { get; set; }
public int countryID { get; set; }
public int stateID { get; set; }
public int cityID { get; set; }
public string pinCode { get; set; }
public DateTime dob { get; set; }
public DateTime doe { get; set; }
public int sourceOfHireID { get; set; }
public int roleID { get; set; }
public bool isActive { get; set; }
public bool maritalStatus { get; set; }
public bool gender { get; set; }
public int setupID { get; set; }
public DateTime joinOn { get; set; }
public int designationID { get; set; }
public int locationID { get; set; }
public Designation designation { get; set; }
public Location location { get; set; }
}
public class Location
{
public int id { get; set; }
public string name { get; set; }
public List<Employee> employees { get; set; }
}
public class Designation
{
public int id { get; set; }
public string name { get; set; }
public DateTime modifiedDate { get; set; }
public DateTime addedDate { get; set; }
public int modifiedBy { get; set; }
public int addedBy { get; set; }
public List<Employee> employees { get; set; }
}
public async Task<ActionResult<IEnumerable<Employee>>> GetEmployee()
{
var tes = _context.Employee.Include(e => e.designation).Include(e => e.location)
.AsNoTracking().Distinct();
return await tes.ToListAsync();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext<YourDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("YourConnectionString")));
}
public class YourDbContext : DbContext
{
public YourDbContext (DbContextOptions<YourDbContext> options)
: base(options)
{
}
public DbSet<TespProj2_2.Models.Employee> Employee { get; set; }
public DbSet<TespProj2_2.Models.Designation> Designation { get; set; }
public DbSet<TespProj2_2.Models.Location> Location { get; set; }
}
Best regards,
Sam
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
75 Points
161 Posts
Json Result getting long
Apr 09, 2020 11:46 AM|Prathamesh Shende|LINK
HEllo I Create a web api in asp.net core
but the result look like so why is it repeating. ? How do i fix this ?
</div> </div> </div> </div> </div> </div> </div>
Contributor
3370 Points
1409 Posts
Re: Json Result getting long
Apr 10, 2020 05:54 AM|samwu|LINK
Hi prathamesh,
I tested this method, it works fine on my side.
This is my tested code:
Best regards,
Sam
Member
75 Points
161 Posts
Re: Json Result getting long
Apr 12, 2020 08:33 AM|Prathamesh Shende|LINK
It working sir. I also want to ask you
is it possible to ignore empty collection
like you can see the result that you posted
"employee": [ ] //I want to ignore this.