How to change the primary key for a EntityData derived class default string Id property to an int property?

asp.net-web-api azure-mobile-services entity-framework-6

Question

I recently started to learn about Azure Mobile Services, I followed this tutorial about it and the classes for my model are required to inherit from the EntityData class.

From the EntityData source code, an Id property is already defined to act as a primary key, but it is defined as string which doesn't work for my Model that uses int.

My class looks like this:

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

If I try to use this class, I get an error message saying an Id property is already defined.

Is there a way to define a different property as a primary key? In case this change is not possible, is there a way to use this string Id property as an incremental primary key?

1
2
1/6/2015 11:43:26 PM

Accepted Answer

The best solution is to use automapper. Here's a blog post that outlines how to do it, essentially you store an int, but transform it into a string when it is sent over the wire:

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

2
1/13/2015 1:11:27 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow