如何通过API在中间表中插入多个值

时间:2019-11-19 07:13:55

标签: linq vue.js .net-core

我经常从VUEJS那里增加价值,在这里编写这样的代码。

<multiselect v-model="schoolTypeform.schoolTypeId" :options="SchoolTypes" :multiple="true" :close-on-select="false" :clear-on-select="false" :preserve-search="true" placeholder="Pick School Type" label="name" track-by="name" :preselect-first="true">

和为此的JS代码是这样写的:

async addSchool() {
            this.isbtnLoading = true;
            this.isException = false;
            await this.axios.post(this.school, this.form).then(response => {
            this.addSchoolType(response.data);
            })
        },
        async addSchoolType(id) {
            this.isbtnLoading = true;
            this.isException = false;
            this.schoolTypeform.shoolId = id;
            await this.axios.post(this.apiBasedUrl + '/SchoolsSchoolType', this.schoolTypeform).then(response => {
             this.isbtnLoading = false; 
            });

现在我的ER结构如下:

学校:(表1)

   public partial class Schools
{
    public Guid ID { get; set; }
    public string Name{ get; set; }

    // Navigation       
    public ICollection<SchoolsSchoolType> SchoolsSchoolTypes { get; set; }     
}

SchoolType :(表2)

    public class SchoolType
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    //Navigation
    public ICollection<SchoolsSchoolType> SchoolsSchoolTypes { get; set; }
}

SchoolsSchoolType(这是中间表):在上面的关系之间是多对多的。

    public class SchoolsSchoolType
{
    public Guid Id { get; set; }

    public Guid ShoolId { get; set; }
    public Schools Schools { get; set; }

    public Guid SchoolTypeId { get; set; }
    public SchoolType SchoolType { get; set; }
}

这里是针对单值输入的存储库方法写入,但是我想在此处在中间表或联结表中添加多个值。

     public async Task<Guid> CreateSchoolsAsync(SchoolsCreateVm schoolsCreateVm)
        {
            if (_GpsContext != null)
             {
                 var schoolsEntity = new Schools()
             {
                 ID = Guid.NewGuid(),
                 Name = schoolsCreateVm.Name, 
                 SchoolsSchoolTypes = new List<SchoolsSchoolType>()
             };

             var schoolType = new SchoolType();
             schoolsEntity.SchoolsSchoolTypes = new List<SchoolsSchoolType>
             {
                 new SchoolsSchoolType
                 {
                     ShoolId =schoolsEntity.ID,
                     SchoolTypeId =schoolType.Id
                 }
             };
             return schoolsEntity.ID;
         }
              return Guid.Empty
}

在此处编写控制器代码:

[HttpPost]
    public async Task<IActionResult> PostSchool([FromBody]SchoolsCreateVm schoolsCreateVm)
    {
        var result = await _schoolsRepository.CreateSchoolsAsync(schoolsCreateVm);
        if (result != null)
        {
            return Ok(result);
        }
        return NotFound();
    }

这是我使用的viewmodel:

 public class SchoolsCreateVm
{
    public string Name { get; set; }
    public List<Guid> SchoolTypeId{ get; set; } // List type of for intermediate table
    public SchoolsCreateVm()
    {
        SchoolTypeId = new List<Guid>();
    }  

如何通过VUEJS多项选择在中间(多对多)关系表中为单个学校插入很多学校类型。 Here the image for multi selection value

1 个答案:

答案 0 :(得分:0)

最后我可以找到解决方案...

#!/bin/bash
readarray -t -d '' FILES < <(git ls-files -z --other --directory)
if [ "$FILES" = "" ]; then
    echo  "Nothing to clean!"
    exit 0
fi
echo -e "Dirty files:\n"
printf '  %s\n' "${FILES[@]}"
DO_REMOVE=0
while true; do
    echo ""
    read -p "Remove ${#FILES[@]} files? [y/n]: " choice
    case "$choice" in
        y|Y )
            DO_REMOVE=1
            break ;;
        n|N )
            echo "Exiting!"
            break ;;
        * ) echo "Invalid input, expected [Y/y/N/n]"
            continue ;;
    esac
done

if [ "$DO_REMOVE" -eq 1 ];then
    echo "Removing!"
    for f in "${FILES[@]}"; do
        rm -rfv -- "$f"
    done
fi