如何在 ASP.NET CORE 中将引用其他两个实体的实体添加到数据库中

发布于 2025-01-09 17:43:07 字数 3089 浏览 0 评论 0 原文

请我将这些实体发送到数据库

MatchingChildtoApplicant  
    { 
        [Key] 
        public int MatchId { get; set; }
        public int ChildId {get; set;}
        public Child  Children { get; set; } 
            
        public int ApplyId {get; set;}
        public ICollection<Application> Applications { get; set; }
            
        public string MatchingBy { get; set; }
     
    }

这是发送到数据库的发布请求

    [HttpPost]
    public async Task<ActionResult<MatchingChildtoApplicant>> AddNewMatching( int applyid, int chd, [FromForm]MatchingChildtoApplicant matchingChildtoApplicant)
    {
         //getting the application to match
         var gettheapplicantion = await _unitOfWork.ApplicationRepository.GetApplicantByIdAsync(applyid);
          if(gettheapplicantion == null){
              return NotFound("The Application to match was not found");
         }
                                
        var appid = gettheapplicantion.ApplyId;
          

        //getting the child to match with applicant
         var childtomatch = await _unitOfWork.ChildRepository.GetChildByIdAsync(chd);
          if(childtomatch == null){
              return NotFound("The child to match was not found");
         }
         var childtoid = childtomatch.Cld;
     
           
        //getting the login user doing the matching
        var userdoingmatch = await _userManager.FindByEmailFromClaimsPrinciple(User);
        if (userdoingmatch == null)
        {
            return NotFound("The user doing the matching details is missing");
        }

        var nameofuser = userdoingmatch.DisplayName;

        //declaring new instance of MatchingChildtoApplicant
        var newmatching = new MatchingChildtoApplicant
        {
             ApplyId =  appid,
             ChildId = childtoid,
             MatchingBy = nameofuser,
            Comment = matchingChildtoApplicant.Comment,
            TheApplicationAcepted = matchingChildtoApplicant.TheApplicationAcepted,
            MatchedDate = matchingChildtoApplicant.MatchedDate,
        };
        

这是我在发送到数据库时收到错误的地方

       //  var result = await _context.MatchingChildtoApplicant.AddAsync(matchingChildtoApplicant);

        // var result = await _context.MatchingChildtoApplicant.AddAsync(matchingChildtoApplicant);
        //   gettheapplicantion.MatchingChildtoApplicants.Children.Add(matchingChildtoApplicant);
        await _context.SaveChangesAsync();
        
        return new MatchingChildtoApplicant
        {
                           
             ApplyId =  appid,
             ChildId = childtoid,
        
            MatchingBy = nameofuser,
            Comment = matchingChildtoApplicant.Comment,
            TheApplicationAcepted = matchingChildtoApplicant.TheApplicationAcepted,
            MatchedDate = matchingChildtoApplicant.MatchedDate,
        };

我得到的错误是这样的 无法从用法中推断出方法“ValueTransformerConfigurationExtensions.Add(List, Expression>)”的类型参数。尝试显式指定类型参数。 [API]

please I have these entities to be sent to database

MatchingChildtoApplicant  
    { 
        [Key] 
        public int MatchId { get; set; }
        public int ChildId {get; set;}
        public Child  Children { get; set; } 
            
        public int ApplyId {get; set;}
        public ICollection<Application> Applications { get; set; }
            
        public string MatchingBy { get; set; }
     
    }

this is the post request to send to the database

    [HttpPost]
    public async Task<ActionResult<MatchingChildtoApplicant>> AddNewMatching( int applyid, int chd, [FromForm]MatchingChildtoApplicant matchingChildtoApplicant)
    {
         //getting the application to match
         var gettheapplicantion = await _unitOfWork.ApplicationRepository.GetApplicantByIdAsync(applyid);
          if(gettheapplicantion == null){
              return NotFound("The Application to match was not found");
         }
                                
        var appid = gettheapplicantion.ApplyId;
          

        //getting the child to match with applicant
         var childtomatch = await _unitOfWork.ChildRepository.GetChildByIdAsync(chd);
          if(childtomatch == null){
              return NotFound("The child to match was not found");
         }
         var childtoid = childtomatch.Cld;
     
           
        //getting the login user doing the matching
        var userdoingmatch = await _userManager.FindByEmailFromClaimsPrinciple(User);
        if (userdoingmatch == null)
        {
            return NotFound("The user doing the matching details is missing");
        }

        var nameofuser = userdoingmatch.DisplayName;

        //declaring new instance of MatchingChildtoApplicant
        var newmatching = new MatchingChildtoApplicant
        {
             ApplyId =  appid,
             ChildId = childtoid,
             MatchingBy = nameofuser,
            Comment = matchingChildtoApplicant.Comment,
            TheApplicationAcepted = matchingChildtoApplicant.TheApplicationAcepted,
            MatchedDate = matchingChildtoApplicant.MatchedDate,
        };
        

This is where I get the error sending to the database

       //  var result = await _context.MatchingChildtoApplicant.AddAsync(matchingChildtoApplicant);

        // var result = await _context.MatchingChildtoApplicant.AddAsync(matchingChildtoApplicant);
        //   gettheapplicantion.MatchingChildtoApplicants.Children.Add(matchingChildtoApplicant);
        await _context.SaveChangesAsync();
        
        return new MatchingChildtoApplicant
        {
                           
             ApplyId =  appid,
             ChildId = childtoid,
        
            MatchingBy = nameofuser,
            Comment = matchingChildtoApplicant.Comment,
            TheApplicationAcepted = matchingChildtoApplicant.TheApplicationAcepted,
            MatchedDate = matchingChildtoApplicant.MatchedDate,
        };

The error I get is this
The type arguments for method 'ValueTransformerConfigurationExtensions.Add(List, Expression<Func<TValue, TValue>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. [API]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文