Wie erhält man ASP.Net Web API und OData, um einen Zeichenfolgenwert als Schlüssel zu binden?

Ich gehe ein kurzes Web Api + OData Tutorial von asp.net durch:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/getting-started-with-odata-in-web-api/create-a-read-only- odata-endpoint.

Ich habe das Beispielprojekt heruntergeladen und es funktioniert. Aber dann fing ich an, mit dem zu spielenProduct Modell, das sie im Beispiel verwenden. Ich habe eine neue Eigenschaft hinzugefügt, die als Schlüssel vom Typ "Zeichenfolge" anstelle eines Ganzzahlschlüssels fungiert.

Die neuen Product.cs:

public class Product
{
    public string stringKey { get; set; }
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
}

Der modifizierte Controller:

public class ProductsController : EntitySetController<Product, string>
{
    static List<Product> products = new List<Product>()
    {
        new Product() { stringKey = "one", ID = 1, Name = "Hat", Price = 15, Category = "Apparel" },
        new Product() { stringKey = "two", ID = 2, Name = "Socks", Price = 5, Category = "Apparel" },
        new Product() { stringKey = "three", ID = 3, Name = "Scarf", Price = 12, Category = "Apparel" },
        new Product() { stringKey = "four", ID = 4, Name = "Yo-yo", Price = 4.95M, Category = "Toys" },
        new Product() { stringKey = "five", ID = 5, Name = "Puzzle", Price = 8, Category = "Toys" },
    };

    [Queryable]
    public override IQueryable<Product> Get()
    {
        return products.AsQueryable();
    }

    protected override Product GetEntityByKey(string key)
    {
        return products.FirstOrDefault(p => p.stringKey == key);
    }
}

Das Problem ist, wenn ich gehe/odata/Products(one) Die Zeichenfolge "Eins" ist nicht an das Schlüsselargument in der gebundenGetEntityByKey(string key) Aktion. Allerdings, wenn ich stöbere zuodata/Products(1) dann wird "1" an die gebundenkey Streit.

Wie kann ich eine Zeichenfolge mit Textwerten korrekt binden lassen, anstatt nur Zeichenfolgen mit numerischen Werten zu binden?

Aktualisieren

Ich habe vergessen, die WebApiConfig einzuschließen:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
        modelBuilder.EntitySet<Product>("Products");

        Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
        config.Routes.MapODataRoute("ODataRoute", "odata", model);
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage