entity-framework - 如何 POST/PATCH 到 Azure 移动服务上的多对多关系实体?

标签 entity-framework automapper azure-mobile-services jointable ef-fluent-api

我一直在关注 the Field Engineer example project从 Windows 开发中心指导如何在我的模型上映射多对多关系。困扰我的是如何将条目插入到多对多关系中。

以我的模型为例:

组织机构可以有很多用户 .

用户 可以属于很多组织 .

我的模型类 用户 看起来像这样:

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; }
}

我的 用户DTO 类是这样的:
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; }

}

我的 组织机构类(class):
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; }
}

我的 组织DTO 类(class):
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; }
}

考虑到这些类,我创建了 Controller 类,我使用 映射了 DTO 和模型类。 AutoMapper 如下:
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,我使用 定义了这两个实体之间的关系。实体类型配置 像这样的类:
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");
        });
    }
}

我创建了一个 表 Controller 处理类(class)用户DTO 组织DTO ,我在插入新用户或新组织时没有问题,但是每个 的端点都没有问题。表 Controller 据我所知,类只允许我单独添加用户或组织。

要在 OrganizationsUser 表中创建一个条目,我该如何实现?

我在想 补丁 request 应该是一种方法,但这是正确的方法吗?我必须为此定义一个 TableController 吗?如何公开此关系中元素的插入、更新、选择和删除?要发送到端点的 JSON 是什么?

编辑 1

我试图在这样的组织中修补用户属性:

网址:serviceUrl/tables/Organization/1

JSON 正文:
{
  "users": [
    {
      "id": "1"
    }
  ]
}

但这给了我一个错误:

The type of one of the primary key values did not match the type defined in the entity. See inner exception for details. Parameter name: keyValues

Inner Exception:

The argument types 'Edm.Int32' and 'Edm.String' are incompatible for this operation. Near WHERE predicate, line 1, column 78.



如果我没记错的话,我似乎需要将发送的字符串映射到使用 Fluent API 创建的连接表中,但是如何映射使用 Fluent API 定义的连接表? PATCH 是做到这一点的方法吗?或者有其他方法吗?

最佳答案

我认为 PATCH 应该可以工作,但我不是 automapper 的专家。

如果每个组织中只有几个用户,另一种选择是在后端创建表示连接表的 View 。有关如何工作的示例,请参阅我在此论坛主题中的帖子:https://social.msdn.microsoft.com/Forums/azure/en-US/4c056373-d5cf-4ca6-9f00-f152d2b01372/best-practice-for-syncing-related-tables-in-offline-mode?forum=azuremobile .这种方法的优点是它确实简化了客户端数据模型,并且不需要了解任何有关外键的信息。

但是,问题是您最终可能会出现重复的用户/组织,这会占用客户端数据库中的更多空间。但是,由于我的示例设置 UpdatedAt 和版本的方式,您将获得所有更新,例如,一旦用户的详细信息发生更改。

关于entity-framework - 如何 POST/PATCH 到 Azure 移动服务上的多对多关系实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870626/

相关文章:

swift - 类型 'MSTable?' 的值没有成员 'pullWithQuery'

c# - 使用 C# 将 Lambda 表达式转换为 SQL UPDATE 语句

entity-framework - EF代码第一个: The EntitySet name 'TestDBContext1.Customers' could not be found

c# - 使用 Autofac 注入(inject)接口(interface)的特定实例

android - 联机 azure 表可以工作,但脱机不能

javascript - 使用 Azure 移动应用程序时出错 - Javascript 客户端 SDK

entity-framework - 属性 'Id'是对象关键信息的一部分,不可修改

c# - 发布应用程序时实体返回 null

c# - 带有构造函数参数的自动映射对象