c# - web api中GET METHOD中的外键问题
问题描述
我有一个表用户,其中有一列带有外键......当我在邮递员中发布 get 或 get by id 的请求时,它也会显示外键表数据。我只想显示“用户”表数据。
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; }
}
}
在邮递员中,当我请求 GET METHOD 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
}
解决方案
您可以为给定创建新User
的这样的Id
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
而不是Model
forResponse
您应该只向客户端公开所需的属性
推荐阅读
- android - 如何从 Android TV 获取 PlayReady DRM 信息?
- elasticsearch - 如何调整 Elasticsearch 以使其快速索引?
- c - 错误:预期的声明说明符或 XXX 之前的“...”(各种参数)
- variables - 常量和静态变量有什么区别,我应该选择哪个?
- mongodb - 如何在 dokku 中设置 mongo 副本集?
- java - 媒体录音机 - Android Java 通话录音接收方声音太低
- anylogic - AnyLogic 教程示例不适合我
- html - 为什么我的图像没有出现在另一个 div 中的 div 中?
- flutter - 无法下载 ddmlib.jar (com.android.tools.ddms:ddmlib:26.1.2)
- jquery - Jquery一次移动单个元素