4

私は、Windows Development Centerのフィールド エンジニアのサンプル プロジェクトに従って、モデルに多対多の関係をマッピングする方法を説明しています。私を悩ませているのは、エントリを多対多の関係に挿入する方法です。

私のモデルを例にとります:

組織は多くのユーザーを持つことができます

ユーザーは多くの組織に所属できます。

Userのモデル クラスは次のようになります。

public class User
{
    public User()
    {
        this.Organizations = new HashSet<Organization>();
    }

    public int Id { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string PasswordSalt { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Active { get; set; }
    public string Picture { get; set; }
    public string PictureMimeType { get; set; }
    public bool Confirmed { get; set; }
    public string ConfirmationKey { get; set; }

    [JsonIgnore]
    public virtual ICollection<Organization> Organizations { get; set; }
}

私のUserDTOクラスは次のとおりです。

public class UserDTO : EntityData
{
    public string Email { get; set; }
    public string Password { get; set; }
    public string PasswordSalt { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Active { get; set; }
    public string Picture { get; set; }
    public string PictureMimeType { get; set; }
    public bool Confirmed { get; set; }
    public string ConfirmationKey { get; set; }

}

私の組織クラス:

public class Organization
{
    public Organization()
    {
        this.Users = new List<User>();
    }

    public string Id { get; set; }
    public string Title { get; set; }
    public string LegalName { get; set; }
    public string TaxReference { get; set; }

    [JsonIgnore]
    public virtual ICollection<User> Users { get; set; }
}

私のOrganizationDTOクラス:

public class OrganizationDTO : EntityData
{
    public string Title { get; set; }
    public string LegalName { get; set; }
    public string TaxReference { get; set; }

    public virtual ICollection<UserDTO> Users { get; set; }
}

これらのクラスを念頭に置いてコントローラー クラスを作成し、次のようにAutoMapperを使用して DTO とモデル クラスをマッピングしました。

cfg.CreateMap<Organization, OrganizationDTO>()
   .ForMember(organizationDTO => organizationDTO.Id, 
      map => map.MapFrom(organization => MySqlFuncs.LTRIM(MySqlFuncs.StringConvert(organization.Id))))
   .ForMember(organizationDTO => organizationDTO.Users, 
      map => map.MapFrom(organization => organization.Users));

 cfg.CreateMap<OrganizationDTO, Organization>()
    .ForMember(organization => organization.Id, 
       map => map.MapFrom(organizationDTO => MySqlFuncs.LongParse(organizationDTO.Id)))
    .ForMember(organization => organization.Users, 
       map => map.Ignore());

Fluent APIを使用して、次のようなEntityTypeConfigurationクラスを使用して、これら 2 つのエンティティ間の関係を定義しました。

public class UserEntityConfiguration : EntityTypeConfiguration<User>
{
    public UserEntityConfiguration()
    {
        this.Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        this.HasMany<Organization>(u => u.Organizations).WithMany(o => o.Users).Map(uo =>
        {
            uo.MapLeftKey("UserId");
            uo.MapRightKey("OrganizationId");
            uo.ToTable("OrganizationsUsers");
        });
    }
}

UserDTOOrganizationDTOを処理するTableControllerクラスを作成しました。新しいUsersまたは新しいOrganizationsを挿入するのに問題はありませんが、各TableControllerクラスのエンドポイントでは、理解できる限り、ユーザーまたは組織を個別に追加することしかできません。

OrganizationsUserテーブルにエントリを作成するには、どうすればこれを達成できますか?

PATCHリクエストがこれを行う方法であるべきだと考えていましたが、それは正しい方法ですか? これには TableController を定義する必要がありますか? この関係で要素の挿入、更新、選択、および削除を公開するにはどうすればよいですか? エンドポイントに送信される JSON は何でしょうか?

編集1

次のような組織のユーザー プロパティにパッチを適用しようとしました。

URL: serviceUrl/tables/Organization/1

JSON 本文:

{
  "users": [
    {
      "id": "1"
    }
  ]
}

しかし、それは私にエラーを与えました:

主キー値の 1 つの型が、エンティティで定義されている型と一致しませんでした。詳細については、内部例外を参照してください。パラメータ名: keyValues

内部例外:

引数の型 'Edm.Int32' と 'Edm.String' は、この操作と互換性がありません。WHERE 述語の近く、1 行目、78 列目。

間違っていなければFluent APIで作成した結合テーブルに送信している文字列をマッピングする必要があるようですが、Fluent APIで定義した結合テーブルをマッピングするにはどうすればよいですか? PATCH はこれを行う方法ですか? それとも他の方法がありますか?

4

1 に答える 1