[英]foreign key issues in GET METHOD in web api
我有一个表用户,它有一个带有外键的列......当我在邮递员的 get 或 get by id 中发布请求时,它也显示了外键表数据。 我只想显示“USERS”表数据。
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Taylor.Models;
namespace Taylor.API_Controller
{
[RoutePrefix("api/Suit")]
public class SuitController : ApiController
{
public TaylorEntities db = new TaylorEntities();
// GET api/<controller>
public List<Suit> GetAllSuit()
{
return db.Suit.ToList();
}
这是一堂课
namespace Taylor.Models
{
using System;
using System.Collections.Generic;
public partial class Users
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Users()
{
this.AppUser = new HashSet<AppUser>();
}
public int UserId { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public int UserRoleId { get; set; }
public bool IsActive { get; set; }
public Nullable<System.DateTime> CreatedAt { get; set; }
public int CreatedBy { get; set; }
public Nullable<System.DateTime> ModifiedAt { get; set; }
public int ModifiedBy { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AppUser> AppUser { get; set; }
public virtual UserRole UserRole { get; set; }
}
}
在邮递员中,当我请求获取方法 https://localhost:44311/api/User/6
它显示了这么多数据
{
"$id": "1",
"AppUser": [],
"UserRole": {
"$id": "2",
"Users": [
{
"$ref": "1"
},
{
"$id": "3",
"AppUser": [
{
"$id": "4",
"Location": {
"$id": "5",
"AppUser": [
{
"$ref": "4"
},
{
"$id": "6",
"Location": {
"$ref": "5"
},
"Users": {
"$ref": "3"
},
"Customer": [],
"AppUserId": 4,
"AdminUserId": 1,
"StartDate": "2021-07-26T04:18:42.893",
"EndDate": "2021-07-26T04:18:42.893",
"LocationId": 1,
"City": "Lahore",
"RegisterDate": "2021-07-26T04:18:42.893",
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
},
{
"$id": "7",
"Location": {
"$ref": "5"
},
"Users": {
"$ref": "3"
},
"Customer": [],
"AppUserId": 5,
"AdminUserId": 1,
"StartDate": "2021-07-26T04:18:42.893",
"EndDate": "2021-07-26T04:18:42.893",
"LocationId": 1,
"City": "Islamabad",
"RegisterDate": "2021-07-26T04:18:42.893",
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
},
{
"$id": "8",
"Location": {
"$ref": "5"
},
"Users": {
"$id": "9",
"AppUser": [
{
"$ref": "8"
}
],
"UserRole": {
"$ref": "2"
},
"UserId": 3,
"Name": "Haider",
"PhoneNumber": "0347-8526942",
"Email": "haider@yahoo.com",
"UserRoleId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
},
"Customer": [
{
"$id": "10",
"AppUser": {
"$ref": "8"
},
"Nap": [
{
"$id": "11",
"Customer": {
"$ref": "10"
},
"NapDetails": [
{
"$id": "12",
"Nap": {
"$ref": "11"
},
"Suit": {
"$id": "13",
"NapDetails": [
{
"$ref": "12"
}
],
"SuitId": 1,
"Title": "Pent",
"IsActive": true,
"CreatedAt": "2021-07-25T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-25T00:00:00",
"ModifiedBy": 1
},
"NapDetailsId": 1,
"SuitId": 1,
"Measurement": "62,25,2514",
"NapId": 1,
"IsActive": true,
"CreatedAt": "2021-07-29T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-29T00:00:00",
"ModifiedBy": 1
},
{
"$id": "14",
"Nap": {
"$ref": "11"
},
"Suit": {
"$id": "15",
"NapDetails": [
{
"$ref": "14"
}
],
"SuitId": 2,
"Title": "Shirt",
"IsActive": true,
"CreatedAt": "2021-07-25T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-25T00:00:00",
"ModifiedBy": 1
},
"NapDetailsId": 2,
"SuitId": 2,
"Measurement": "fbsh,2514785",
"NapId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
}
],
"NapId": 1,
"CustomerId": 1,
"ImageUrl": "sgjakdvaksgfew",
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
},
{
"$id": "16",
"Customer": {
"$ref": "10"
},
"NapDetails": [],
"NapId": 4,
"CustomerId": 1,
"ImageUrl": "sgjakdvaksgfew",
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
}
],
"CustomerId": 1,
"AppUserId": 3,
"PhoneNumber": "0321-8521475",
"CustomerName": "Talha",
"Address": "Multan",
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
},
{
"$id": "17",
"AppUser": {
"$ref": "8"
},
"Nap": [],
"CustomerId": 3,
"AppUserId": 3,
"PhoneNumber": "0321-8521475",
"CustomerName": "Abu Bakar",
"Address": "Multan",
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
}
],
"AppUserId": 3,
"AdminUserId": 3,
"StartDate": "2021-07-26T00:00:00",
"EndDate": "2021-07-31T00:00:00",
"LocationId": 1,
"City": "karachi",
"RegisterDate": "2001-12-15T00:00:00",
"IsActive": true,
"CreatedAt": "2021-07-26T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T00:00:00",
"ModifiedBy": 1
}
],
"Location1": {
"$ref": "5"
},
"Location2": {
"$ref": "5"
},
"LocationId": 1,
"Lat": 0.254,
"Long": 0.568,
"IsActive": true,
"CreatedAt": "2021-07-15T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-15T00:00:00",
"ModifiedBy": 1
},
"Users": {
"$ref": "3"
},
"Customer": [],
"AppUserId": 1,
"AdminUserId": 1,
"StartDate": "2021-06-15T00:00:00",
"EndDate": "2021-07-15T00:00:00",
"LocationId": 1,
"City": "multan",
"RegisterDate": "2001-06-24T00:00:00",
"IsActive": true,
"CreatedAt": "2021-07-15T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-15T00:00:00",
"ModifiedBy": 1
},
{
"$ref": "6"
},
{
"$ref": "7"
}
],
"UserRole": {
"$ref": "2"
},
"UserId": 1,
"Name": "Khakan",
"PhoneNumber": "0300-1234567",
"Email": "khakan@gmail.com",
"UserRoleId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T00:00:00",
"ModifiedBy": 1
},
{
"$ref": "9"
},
{
"$id": "18",
"AppUser": [],
"UserRole": {
"$ref": "2"
},
"UserId": 4,
"Name": "Shahzad",
"PhoneNumber": "0347-8526942",
"Email": "shahzad@hotmail.com",
"UserRoleId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T00:00:00",
"ModifiedBy": 1
},
{
"$id": "19",
"AppUser": [],
"UserRole": {
"$ref": "2"
},
"UserId": 7,
"Name": "Mr. Johny",
"PhoneNumber": "0300-6589321",
"Email": "johny@hotmail.com",
"UserRoleId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
},
{
"$id": "20",
"AppUser": [],
"UserRole": {
"$ref": "2"
},
"UserId": 8,
"Name": "Usman Khan",
"PhoneNumber": "0321-9632584",
"Email": "usman@gmail.com",
"UserRoleId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
}
],
"UserRoleId": 1,
"Title": "Admin",
"IsActive": true,
"CreatedAt": "2021-07-26T00:00:00",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T00:00:00",
"ModifiedBy": 1
},
"UserId": 6,
"Name": "Awais",
"PhoneNumber": "0321-9632584",
"Email": "awais@hotmail.com",
"UserRoleId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
}
但我只想要这个
"UserId": 6,
"Name": "Awais",
"PhoneNumber": "0321-9632584",
"Email": "awais@hotmail.com",
"UserRoleId": 1,
"IsActive": true,
"CreatedAt": "2021-07-26T04:18:42.893",
"CreatedBy": 1,
"ModifiedAt": "2021-07-26T04:18:42.893",
"ModifiedBy": 1
}
您需要提供有关在后端使用什么框架与数据库交互的更多信息。
如果使用了实体框架,可能从数据库中获取结果的查询具有“Include”扩展方法,这就是它也获取外部数据的原因。
您可以为给定的Id
创建这样的新User
var user = conetxt.Users
.Select(u=> new User{ UserId = u.UserId, UserName = u.UserName, ... })
.SingleOrDefault(u=>u.UserId == id);
如果你想要List<User>
var user = conetxt.Users
.Select(u=> new User{ UserId = u.UserId, UserName = u.UserName, ... })
.ToList();
建议使用
DTO
而不是Response
Model
您应该只向客户端公开必需的属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.