You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建含关联服务与员工的企业时实体关系配置异常

问题:创建Business实体时无法正确关联Service与Worker

尝试通过包含关联Service和Worker的JSON请求体创建新Business实体时,Business的Services和Workers集合未能正确建立关联。调试发现,Services和Workers集合的Business、Workers属性均为null,关系映射存在问题。

移除模型中的[JsonIgnore]特性后,传入以下JSON请求体:

{
  "BusinessUsername": "new_business",
  "BusinessName": "New Business Name",
  "BusinessPhone": "987-654-3210",
  "BusinessEmail": "newbusiness@example.com",
  "BusinessLocation": "456 Elm Street",
  "BusinessDescription": "Description of the new business",
  "BusinessCategory": "New Category",
  "OwnerFirstName": "Jane",
  "OwnerLastName": "Smith",
  "BusinessPassword": "securepassword123",
  "BusinessEstablishedDate": "2022-02-15",
  "Services": [
    {
      "ServiceName": "Service A",
      "Description": "Description of Service A",
      "Duration": 60,
      "Price": 50.00
    },
    {
      "ServiceName": "Service B",
      "Description": "Description of Service B",
      "Duration": 30,
      "Price": 35.00
    }
  ],
  "Workers": [
    {
      "WorkerFirstName": "John",
      "WorkerLastName": "Doe",
      "WorkerPhone": "555-123-4567",
      "WorkerEmail": "johndoe@example.com"
    },
    {
      "WorkerFirstName": "Alice",
      "WorkerLastName": "Smith",
      "WorkerPhone": "555-987-6543",
      "WorkerEmail": "alicesmith@example.com"
    }
  ]
}

收到如下400验证错误:

{ "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "00-243f2a7070d552e248f5ea551282134b-4fa947568f710590-00", "errors": { "Workers[0].Business": [ "The Business field is required." ], "Workers[0].Services": [ "The Services field is required." ], "Workers[1].Business": [ "The Business field is required." ], "Workers[1].Services": [ "The Services field is required." ], "Services[0].Workers": [ "The Workers field is required." ], "Services[0].Business": [ "The Business field is required." ], "Services[1].Workers": [ "The Workers field is required." ], "Services[1].Business": [ "The Business field is required." ] } }

相关代码

Business模型

using Microsoft.AspNetCore.Identity;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public class Business : IdentityUser
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonIgnore]
    public int BusinessID { get; set; }

    [Required(ErrorMessage = "Username is required")]
    public string BusinessUsername { get; set; }

    [Required]
    public string BusinessName { get; set; }

    [Required]
    public string BusinessPhone { get; set; }

    [Required]
    public string BusinessEmail { get; set; }

    [Required]
    public string BusinessLocation { get; set; }

    [Required]
    public string BusinessDescription { get; set; }

    [Required]
    public string BusinessCategory { get; set; }

    [Required(ErrorMessage = "First name is required")]
    public string OwnerFirstName { get; set; }

    [Required(ErrorMessage = "Last name is required")]
    public string OwnerLastName { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [MinLength(6, ErrorMessage = "Password must be at least 6 characters long")]
    public string BusinessPassword { get; set; }

    [Required]
    public DateTime BusinessEstablishedDate { get; set; }

    // Navigation property to represent the services offered by the business
    [JsonIgnore]
    public ICollection<Service> Services { get; set; }

    // Navigation property to represent the workers employed by the business
    [JsonIgnore]
    public ICollection<Worker> Workers { get; set; }
}

Service模型

using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;

public class Service
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonIgnore]
    public int ServiceID { get; set; }

    [Required]
    public string ServiceName { get; set; }

    public string Description { get; set; }

    [Required]
    public int Duration { get; set; }

    [Required]
    public decimal Price { get; set; }


    [Required]
    [JsonIgnore]
    public int BusinessId { get; set; } // Foreign key to associate service with a business

    // Navigation property to represent the relationship with business
    [JsonIgnore]
    public Business Business { get; set; }

    // Navigation property to represent the workers providing this service
    [JsonIgnore]
    public ICollection<Worker> Workers { get; set; }
}

Worker模型

using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;

public class Worker
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonIgnore]
    public int WorkerID { get; set; }

    [Required]
    public string WorkerFirstName { get; set; }

    [Required]
    public string WorkerLastName { get; set; }

    [Required]
    [Phone]
    public string WorkerPhone { get; set; }

    [Required]
    [EmailAddress]
    public string WorkerEmail { get; set; }

    [Required]
    [JsonIgnore]
    public int BusinessId { get; set; } // Foreign key to associate worker with a business

    // Navigation property to represent the relationship with business
    [JsonIgnore]
    public Business Business { get; set; }

    // Navigation property to represent the services the worker can provide
    [JsonIgnore]
    public ICollection<Service> Services { get; set; }
}

Business服务

using AppointSmart.Data;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class BusinessService : IBusiness
{
    private readonly ApplicationDbContext _context;

    public BusinessService(ApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<Business> GetBusinessByIdAsync(int businessId)
    {
        return await _context.Businesses.FindAsync(businessId);
    }

    public async Task<Business> RegisterBusinessAsync(Business businessModel)
    {
        // Validate input (e.g., check for required fields, password strength, etc.)

        // Hash the user's password

        // Create a new User object
        var newUser = new Business
        {
            BusinessUsername = businessModel.BusinessUsername,
            BusinessName = businessModel.BusinessName,
            BusinessPhone = businessModel.BusinessPhone,
            BusinessEmail = businessModel.BusinessEmail,
            BusinessLocation = businessModel.BusinessLocation,
            BusinessDescription = businessModel.BusinessDescription,
            BusinessCategory = businessModel.BusinessCategory,
            OwnerFirstName = businessModel.OwnerFirstName,
            OwnerLastName = businessModel.OwnerLastName,
            BusinessPassword = businessModel.BusinessPassword,
            BusinessEstablishedDate = businessModel.BusinessEstablishedDate
        };

        // Add the user to the database
        _context.Businesses.Add(newUser);
        try
        {
            await _context.SaveChangesAsync();
            return newUser;
        }
        catch (DbUpdateException ex)
        {
            // Handle database exception
            throw new ApplicationException("Error occurred while registering the business.", ex);
        }
    }

    public async Task<Business> GetBusinessByUsernameAsync(string businessUsername)
    {
        return await _context.Businesses.FirstOrDefaultAsync(b => b.BusinessUsername == businessUsername);
    }

    public async Task<Business> GetBusinessByEmailAsync(string businessEmail)
    {
        return await _context.Businesses.FirstOrDefaultAsync(b => b.BusinessEmail == businessEmail);
    }

    public async Task<Business> UpdateBusinessAsync(int businessId, Business business)
    {
        var existingBusiness = await _context.Businesses.FindAsync(businessId);

        if (existingBusiness == null)
        {
            throw new ApplicationException("Business not found");
        }

        existingBusiness.BusinessName = business.BusinessName;
        existingBusiness.BusinessPhone = business.BusinessPhone;
        existingBusiness.BusinessEmail = business.BusinessEmail;
        existingBusiness.BusinessLocation = business.BusinessLocation;
        existingBusiness.BusinessDescription = business.BusinessDescription;
        existingBusiness.BusinessCategory = business.BusinessCategory;
        existingBusiness.OwnerFirstName = business.OwnerFirstName;
        existingBusiness.OwnerLastName = business.OwnerLastName;

        await _context.SaveChangesAsync();

        return existingBusiness;
    }

    public async Task<bool> DeleteBusinessAsync(int businessId)
    {
        var existingBusiness = await _context.Businesses.FindAsync(businessId);

        if (existingBusiness == null)
        {
            throw new ApplicationException("Business not found");
        }

        _context.Businesses.Remove(existingBusiness);
        await _context.SaveChangesAsync();

        return true;
    }
}

Business控制器

using AppointSmart.Data;
using AppointSmart.Interfaces;
using Microsoft.AspNetCore.Mvc;
using System.Web.Http.ModelBinding;

namespace AppointSmart.Controllers
{
    [ApiController]
    public class BusinessRegistrationController : ControllerBase
    {
        private readonly ApplicationDbContext _context;
        private readonly IBusiness _businessService;

        public BusinessRegistrationController(ApplicationDbContext context, IBusiness businessService)
        {
            _businessService = businessService;
            _context = context;
        }

        // Endpoint for user registration
        [HttpPost("businessRegister")]
        public async Task<IActionResult> BusinessRegisterAsync([FromBody] Business businessModel)
        {
            try
            {
                // Attempt to register the user
                var registeredBusiness = await _businessService.RegisterBusinessAsync(businessModel);

                // Return the newly created user (you can also return a success message)
                return CreatedAtAction(nameof(GetBusinessByIdAsync), new { userId = registeredBusiness.Id }, registeredBusiness);
            }
            catch (Exception ex)
            {
                // Handle exceptions and return an appropriate error response
                return StatusCode(500, $"Internal server error: {ex.Message}");
            }
        }

        [HttpPut]
        [Route("UpdateBusinessAsync/{businessId}")]
        public async Task<IActionResult> UpdateBusinessAsync(int businessId, Business business)
        {
            try
            {
                var result = await _businessService.UpdateBusinessAsync(businessId, business);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"An error occurred: {ex.Message}");
            }
        }

        [HttpDelete]
        [Route("DeleteBusinessAsync/{businessId}")]
        public async Task<IActionResult> DeleteBusinessAsync(int businessId)
        {
            try
            {
                var result = await _businessService.DeleteBusinessAsync(businessId);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"An error occurred: {ex.Message}");
            }
        }

        // Endpoint for user login
        [HttpGet("GetBusinessByIdAsync/{businessId}")]
        public async Task<IActionResult> GetBusinessByIdAsync(int businessId)
        {
            try
            {
                var user = await _businessService.GetBusinessByIdAsync(businessId);
                if (user == null)
                {
                    return NotFound();
                }
                return Ok(user);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"Internal server error: {ex.Message}");
            }
        }

        // Endpoint for getting user by username
        [HttpGet("GetBusinessByUsernameAsync/{businessUsername}")]
        public async Task<IActionResult> GetBusinessByUsernameAsync(string username)
        {
            var user = await _businessService.GetBusinessByUsernameAsync(username);
            if (user == null)
            {
                return NotFound();
            }

            return Ok(user);
        }

        // Endpoint for getting user by email
        [HttpGet("GetBusinessByEmailAsync/{businessEmail}")]
        public async Task<IActionResult> GetBusinessByEmailAsync(string email)
        {
            var user = await _businessService.GetBusinessByEmailAsync(email);
            if (user == null)
            {
                return NotFound();
            }

            return Ok(user);
        }
    }
}

内容的提问来源于stack exchange,提问作者Vaclemor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 13:32:02