如何通过Postman创建Clothes对象?解决必填字段报错问题
问题:Postman创建Clothes对象时提示Designer_Clothes和FashionHouse为必填项,请求失败
我在通过Postman创建Clothes对象时,系统提示Designer_Clothes和FashionHouse字段为必填项,请求无法成功执行。
实体模型代码
Clothes类:
public class Clothes:IEntityBase { [Key] public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public double Price { get; set; } public string ImageUrl { get; set; } public ClothesCategory ClothesCategory { get; set; } public List<Designer_Clothes> Designer_Clothes { get; set; } public int FashionHouseId { get; set; } [ForeignKey("FashionHouseId")] public FashionHouse FashionHouse { get; set; } }
Designer类:
public class Designer:IEntityBase { [HiddenInput] [Key] public int Id { get; set; } [Display(Name = "Profile Picture")] [Required(ErrorMessage ="Profile picture is required")] public string ProfilePictureUrl { get; set; } [Display(Name = "Full Name")] [Required(ErrorMessage ="Full name is required")] [StringLength(25, MinimumLength =3,ErrorMessage ="Full name must be between 3 and 25 chars")] public string FullName { get; set; } [Display(Name = "Bio")] [Required(ErrorMessage ="Biography is required")] public string Bio { get; set; } public List<Designer_Clothes>? Designer_Clothes { get; set; } }
Designer_Clothes类:
public class Designer_Clothes { public int ClothesId { get; set; } public Clothes Clothes { get; set; } public int DesignerId { get; set; } public Designer Designer { get; set; } }
FashionHouse类:
public class FashionHouse:IEntityBase { [Key] public int Id { get; set; } [Display(Name ="Profile picture")] [Required(ErrorMessage ="Profile picture is required")] public string ProfilePictureUrl { get; set; } [Display(Name ="Full Name")] [StringLength(50,MinimumLength =3,ErrorMessage ="Full name must be between 3 and 50 chars")] [Required(ErrorMessage = "Full name is required")] public string FullName { get; set; } [Display(Name = "Bio")] [Required(ErrorMessage = "Biography is required")] public string Bio { get; set; } public List<Clothes>? Clothes { get; set; } }
控制器Post方法
[HttpPost] public async Task<IActionResult> Create([FromBody] Clothes clothes) { if (!ModelState.IsValid) { return BadRequest(); } await _service.AddAsync(clothes); return Created($"/api/book/{clothes.Id}", clothes); }
提交的JSON请求体
{ "name": "Second item ", "description": "Description of second item test", "price": 50, "imageUrl": "sdfdsfsdfsf", "clothesCategory": 3, "designer_Clothes": [{ "ClothesId":1, "DesignerId":2 }], "fashionHouseId": 2, "fashionHouse": { "Bio":"TEST", "FullName":"TESTE", "ProfilePictureUrl":"ASdASd" } }
解决方案
1. 修复实体模型的可空性问题
EF Core默认会将未标记为可空的引用类型视为必填项,导致模型验证失败。修改Clothes类,将关联属性标记为可空:
public class Clothes:IEntityBase { [Key] public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public double Price { get; set; } public string ImageUrl { get; set; } public ClothesCategory ClothesCategory { get; set; } // 标记为可空集合 public List<Designer_Clothes>? Designer_Clothes { get; set; } public int FashionHouseId { get; set; } [ForeignKey("FashionHouseId")] // 标记导航属性为可空 public FashionHouse? FashionHouse { get; set; } }
2. 调整请求体,移除冗余数据
- 已传递
fashionHouseId:2,无需再传递完整的fashionHouse对象,否则EF会尝试创建新的FashionHouse实例,同时触发该对象的必填字段验证。 - 新创建的Clothes还未生成ID,不需要手动指定
Designer_Clothes中的ClothesId。
修改后的请求体:
{ "name": "Second item", "description": "Description of second item test", "price": 50, "imageUrl": "sdfdsfsdfsf", "clothesCategory": 3, "designer_Clothes": [{ "DesignerId": 2 }], "fashionHouseId": 2 }
3. 配置EF Core关联关系
确保DbContext中正确配置Designer_Clothes的复合主键和关联规则,避免数据操作异常:
protected override void OnModelCreating(ModelBuilder modelBuilder) { // 配置复合主键 modelBuilder.Entity<Designer_Clothes>() .HasKey(dc => new { dc.ClothesId, dc.DesignerId }); // 配置Clothes与Designer_Clothes的关联 modelBuilder.Entity<Designer_Clothes>() .HasOne(dc => dc.Clothes) .WithMany(c => c.Designer_Clothes) .HasForeignKey(dc => dc.ClothesId); // 配置Designer与Designer_Clothes的关联 modelBuilder.Entity<Designer_Clothes>() .HasOne(dc => dc.Designer) .WithMany(d => d.Designer_Clothes) .HasForeignKey(dc => dc.DesignerId); }
4. 返回具体验证错误排查问题
修改控制器方法,返回详细的模型验证错误,方便定位问题:
[HttpPost] public async Task<IActionResult> Create([FromBody] Clothes clothes) { if (!ModelState.IsValid) { // 返回具体错误信息 return BadRequest(ModelState); } await _service.AddAsync(clothes); return Created($"/api/clothes/{clothes.Id}", clothes); }
内容的提问来源于stack exchange,提问作者Ptrk12
相关产品推荐
相关产品推荐

