如何将实体数据派生类默认字符串 Id 属性的主键更改为 int 属性



我最近开始了解 Azure 移动服务,我遵循了本教程,我的模型的类需要从 EntityData 类继承。

从 EntityData 源代码中,Id 属性已定义为充当主键,但它被定义为字符串,不适用于使用 int 的模型。

我的类看起来像这样:

public partial class Role : EntityData
{
    public Role()
    {
        this.Users = new HashSet<User>();
    }
    [Key]
    public int RoleId { get; set; }
    public string Title { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

如果我尝试使用此类,则会收到一条错误消息,指出已定义 Id 属性。

有没有办法将不同的属性定义为主键?如果无法进行此更改,是否有办法将此字符串 Id 属性用作增量主键?

最好的解决方案是使用自动映射器。这里有一篇博客文章概述了如何做到这一点,本质上你存储一个 int,但在通过网络发送时将其转换为字符串:

http://blogs.msdn.com/b/azuremobile/archive/2014/05/22/tables-with-integer-keys-and-the-net-backend.aspx

最新更新