How to set enable alternate keys with Microsoft.AspNet.OData version 6












0















In Microsoft.AspNet.OData V5.9.1 and lower, you could enable alternate keys by calling EnableAlternateKeys() in the WebApiConfig.cs like so:



config.EnableAlternateKeys(true);


This option has been removed along with others like :



config.EnableCaseInsensitive(true);


How do you turn the EnableAlternateKeys option on in Microsoft.AspNet.OData V6+?










share|improve this question























  • The same happened with the EnableEnumPrefixFree method. For the last one, it seems an issue was raised in the OData github repo: link. You might comment there as well

    – jonathan.meesschaert
    Feb 2 '17 at 12:56
















0















In Microsoft.AspNet.OData V5.9.1 and lower, you could enable alternate keys by calling EnableAlternateKeys() in the WebApiConfig.cs like so:



config.EnableAlternateKeys(true);


This option has been removed along with others like :



config.EnableCaseInsensitive(true);


How do you turn the EnableAlternateKeys option on in Microsoft.AspNet.OData V6+?










share|improve this question























  • The same happened with the EnableEnumPrefixFree method. For the last one, it seems an issue was raised in the OData github repo: link. You might comment there as well

    – jonathan.meesschaert
    Feb 2 '17 at 12:56














0












0








0


0






In Microsoft.AspNet.OData V5.9.1 and lower, you could enable alternate keys by calling EnableAlternateKeys() in the WebApiConfig.cs like so:



config.EnableAlternateKeys(true);


This option has been removed along with others like :



config.EnableCaseInsensitive(true);


How do you turn the EnableAlternateKeys option on in Microsoft.AspNet.OData V6+?










share|improve this question














In Microsoft.AspNet.OData V5.9.1 and lower, you could enable alternate keys by calling EnableAlternateKeys() in the WebApiConfig.cs like so:



config.EnableAlternateKeys(true);


This option has been removed along with others like :



config.EnableCaseInsensitive(true);


How do you turn the EnableAlternateKeys option on in Microsoft.AspNet.OData V6+?







asp.net-web-api odata






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 30 '17 at 5:12









Matt SpradleyMatt Spradley

3,34282437




3,34282437













  • The same happened with the EnableEnumPrefixFree method. For the last one, it seems an issue was raised in the OData github repo: link. You might comment there as well

    – jonathan.meesschaert
    Feb 2 '17 at 12:56



















  • The same happened with the EnableEnumPrefixFree method. For the last one, it seems an issue was raised in the OData github repo: link. You might comment there as well

    – jonathan.meesschaert
    Feb 2 '17 at 12:56

















The same happened with the EnableEnumPrefixFree method. For the last one, it seems an issue was raised in the OData github repo: link. You might comment there as well

– jonathan.meesschaert
Feb 2 '17 at 12:56





The same happened with the EnableEnumPrefixFree method. For the last one, it seems an issue was raised in the OData github repo: link. You might comment there as well

– jonathan.meesschaert
Feb 2 '17 at 12:56












2 Answers
2






active

oldest

votes


















2














This has changed in OData WebAPI 6. Since v6 Dependency Injection has been introduced (see docs). This means the old way of configuring your service has been changed. From now on you should register all necessary services and routes in the MapODataServiceRoute method



public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

setErrorDetailPolicy();
var model = BuildEdmModel();

// Web API routes
config.MapODataServiceRoute("routeName", "routePrefix", builder =>
builder.AddDefaultODataServices()
.AddService<IEdmModel>(ServiceLifetime.Singleton, s => model)
.AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => ODataRoutingConventions.CreateDefaultWithAttributeRouting("routeContact", config))
.AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))
);
}


The important line here is the last one:



.AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))


After this is enabled I found that my model had issues recognizing the AddAlternateKeyAnnotation extension method. This was solved by casting the instance to an EdmModel (instead of using the IEdmModel it was). My ModelBuilder class looks something like this:



    internal static IEdmModel GetModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = "MyNamespace";

var personsSet = builder.EntitySet<PersonDTO>("Persons");
builder.EntityType<PersonDTO>().HasKey(k => k.Id);

var model = builder.GetEdmModel();

// model is built, add alternateKey annotation as necessary
// first, find entity type
IEdmEntityType t = model.FindDeclaredEntitySet("Persons").EntityType();

// now find the properties we want to use as alternateKey
var firstNameProp = t.FindProperty("FirstName");
var lastNameProp = t.FindProperty("LastName");

// and finally add the annotation
((EdmModel)model).AddAlternateKeyAnnotation(t, new Dictionary<string, IEdmProperty> {
{
"FirstName", firstNameProp
},
{
"LastName", lastNameProp
}
});

return model;
}


Note that there are still a couple of convenience methods. From the documentation:




You might also find that we still preserve the previous overloads of
MapODataServiceRoute which take batch handlers, path handlers, HTTP
message handlers, etc. They are basically wrapping the first overload
that takes a configureAction. The reason why we keep them is that we
want to give the users convenience to create OData services and
bearings to the APIs they are familiar with.




If you're wondering what the first line does:



config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);


This is because in OData v6 by default all query options are disabled (again, see docs). You should either enable them on each controller/action, or do as I did, and enable them globally (which is what the above line does).






share|improve this answer


























  • I don't see how what you've shown above re-enables the AddAlternateKeyAnnotation functionality for models.

    – Matt Spradley
    Mar 4 '17 at 16:51











  • Note that the modelbuilding part is different from the configuration of the OData service. Your question seemed to be only about the configuration part. Anyway, adding the alternateKey annotation should work the same as it used to. See updated answer

    – jonathan.meesschaert
    Mar 6 '17 at 8:59



















0














Something missing from jonathan.meesschaert answer is the signature for your action in the OData controller.



In my case I had the following alernate key:



((EdmModel)edmModel).AddAlternateKeyAnnotation(
entityType,
new Dictionary<string, IEdmProperty>() {
{ "Text", edmProperty }
});


and the asociated controller action was:



public MenuItem Get([FromODataUri]string keyText)


Note that you don't need to use [HttpGet] or [ODataRoute("Menus(Text={Text})")] to get it working. However, the crucial part is that I had to append key to the parameter name. Without that it didn't work on OData v7.1.0






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41929367%2fhow-to-set-enable-alternate-keys-with-microsoft-aspnet-odata-version-6%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    This has changed in OData WebAPI 6. Since v6 Dependency Injection has been introduced (see docs). This means the old way of configuring your service has been changed. From now on you should register all necessary services and routes in the MapODataServiceRoute method



    public static void Register(HttpConfiguration config)
    {
    // Web API configuration and services
    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

    setErrorDetailPolicy();
    var model = BuildEdmModel();

    // Web API routes
    config.MapODataServiceRoute("routeName", "routePrefix", builder =>
    builder.AddDefaultODataServices()
    .AddService<IEdmModel>(ServiceLifetime.Singleton, s => model)
    .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => ODataRoutingConventions.CreateDefaultWithAttributeRouting("routeContact", config))
    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))
    );
    }


    The important line here is the last one:



    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))


    After this is enabled I found that my model had issues recognizing the AddAlternateKeyAnnotation extension method. This was solved by casting the instance to an EdmModel (instead of using the IEdmModel it was). My ModelBuilder class looks something like this:



        internal static IEdmModel GetModel()
    {
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.Namespace = "MyNamespace";

    var personsSet = builder.EntitySet<PersonDTO>("Persons");
    builder.EntityType<PersonDTO>().HasKey(k => k.Id);

    var model = builder.GetEdmModel();

    // model is built, add alternateKey annotation as necessary
    // first, find entity type
    IEdmEntityType t = model.FindDeclaredEntitySet("Persons").EntityType();

    // now find the properties we want to use as alternateKey
    var firstNameProp = t.FindProperty("FirstName");
    var lastNameProp = t.FindProperty("LastName");

    // and finally add the annotation
    ((EdmModel)model).AddAlternateKeyAnnotation(t, new Dictionary<string, IEdmProperty> {
    {
    "FirstName", firstNameProp
    },
    {
    "LastName", lastNameProp
    }
    });

    return model;
    }


    Note that there are still a couple of convenience methods. From the documentation:




    You might also find that we still preserve the previous overloads of
    MapODataServiceRoute which take batch handlers, path handlers, HTTP
    message handlers, etc. They are basically wrapping the first overload
    that takes a configureAction. The reason why we keep them is that we
    want to give the users convenience to create OData services and
    bearings to the APIs they are familiar with.




    If you're wondering what the first line does:



    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);


    This is because in OData v6 by default all query options are disabled (again, see docs). You should either enable them on each controller/action, or do as I did, and enable them globally (which is what the above line does).






    share|improve this answer


























    • I don't see how what you've shown above re-enables the AddAlternateKeyAnnotation functionality for models.

      – Matt Spradley
      Mar 4 '17 at 16:51











    • Note that the modelbuilding part is different from the configuration of the OData service. Your question seemed to be only about the configuration part. Anyway, adding the alternateKey annotation should work the same as it used to. See updated answer

      – jonathan.meesschaert
      Mar 6 '17 at 8:59
















    2














    This has changed in OData WebAPI 6. Since v6 Dependency Injection has been introduced (see docs). This means the old way of configuring your service has been changed. From now on you should register all necessary services and routes in the MapODataServiceRoute method



    public static void Register(HttpConfiguration config)
    {
    // Web API configuration and services
    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

    setErrorDetailPolicy();
    var model = BuildEdmModel();

    // Web API routes
    config.MapODataServiceRoute("routeName", "routePrefix", builder =>
    builder.AddDefaultODataServices()
    .AddService<IEdmModel>(ServiceLifetime.Singleton, s => model)
    .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => ODataRoutingConventions.CreateDefaultWithAttributeRouting("routeContact", config))
    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))
    );
    }


    The important line here is the last one:



    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))


    After this is enabled I found that my model had issues recognizing the AddAlternateKeyAnnotation extension method. This was solved by casting the instance to an EdmModel (instead of using the IEdmModel it was). My ModelBuilder class looks something like this:



        internal static IEdmModel GetModel()
    {
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.Namespace = "MyNamespace";

    var personsSet = builder.EntitySet<PersonDTO>("Persons");
    builder.EntityType<PersonDTO>().HasKey(k => k.Id);

    var model = builder.GetEdmModel();

    // model is built, add alternateKey annotation as necessary
    // first, find entity type
    IEdmEntityType t = model.FindDeclaredEntitySet("Persons").EntityType();

    // now find the properties we want to use as alternateKey
    var firstNameProp = t.FindProperty("FirstName");
    var lastNameProp = t.FindProperty("LastName");

    // and finally add the annotation
    ((EdmModel)model).AddAlternateKeyAnnotation(t, new Dictionary<string, IEdmProperty> {
    {
    "FirstName", firstNameProp
    },
    {
    "LastName", lastNameProp
    }
    });

    return model;
    }


    Note that there are still a couple of convenience methods. From the documentation:




    You might also find that we still preserve the previous overloads of
    MapODataServiceRoute which take batch handlers, path handlers, HTTP
    message handlers, etc. They are basically wrapping the first overload
    that takes a configureAction. The reason why we keep them is that we
    want to give the users convenience to create OData services and
    bearings to the APIs they are familiar with.




    If you're wondering what the first line does:



    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);


    This is because in OData v6 by default all query options are disabled (again, see docs). You should either enable them on each controller/action, or do as I did, and enable them globally (which is what the above line does).






    share|improve this answer


























    • I don't see how what you've shown above re-enables the AddAlternateKeyAnnotation functionality for models.

      – Matt Spradley
      Mar 4 '17 at 16:51











    • Note that the modelbuilding part is different from the configuration of the OData service. Your question seemed to be only about the configuration part. Anyway, adding the alternateKey annotation should work the same as it used to. See updated answer

      – jonathan.meesschaert
      Mar 6 '17 at 8:59














    2












    2








    2







    This has changed in OData WebAPI 6. Since v6 Dependency Injection has been introduced (see docs). This means the old way of configuring your service has been changed. From now on you should register all necessary services and routes in the MapODataServiceRoute method



    public static void Register(HttpConfiguration config)
    {
    // Web API configuration and services
    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

    setErrorDetailPolicy();
    var model = BuildEdmModel();

    // Web API routes
    config.MapODataServiceRoute("routeName", "routePrefix", builder =>
    builder.AddDefaultODataServices()
    .AddService<IEdmModel>(ServiceLifetime.Singleton, s => model)
    .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => ODataRoutingConventions.CreateDefaultWithAttributeRouting("routeContact", config))
    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))
    );
    }


    The important line here is the last one:



    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))


    After this is enabled I found that my model had issues recognizing the AddAlternateKeyAnnotation extension method. This was solved by casting the instance to an EdmModel (instead of using the IEdmModel it was). My ModelBuilder class looks something like this:



        internal static IEdmModel GetModel()
    {
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.Namespace = "MyNamespace";

    var personsSet = builder.EntitySet<PersonDTO>("Persons");
    builder.EntityType<PersonDTO>().HasKey(k => k.Id);

    var model = builder.GetEdmModel();

    // model is built, add alternateKey annotation as necessary
    // first, find entity type
    IEdmEntityType t = model.FindDeclaredEntitySet("Persons").EntityType();

    // now find the properties we want to use as alternateKey
    var firstNameProp = t.FindProperty("FirstName");
    var lastNameProp = t.FindProperty("LastName");

    // and finally add the annotation
    ((EdmModel)model).AddAlternateKeyAnnotation(t, new Dictionary<string, IEdmProperty> {
    {
    "FirstName", firstNameProp
    },
    {
    "LastName", lastNameProp
    }
    });

    return model;
    }


    Note that there are still a couple of convenience methods. From the documentation:




    You might also find that we still preserve the previous overloads of
    MapODataServiceRoute which take batch handlers, path handlers, HTTP
    message handlers, etc. They are basically wrapping the first overload
    that takes a configureAction. The reason why we keep them is that we
    want to give the users convenience to create OData services and
    bearings to the APIs they are familiar with.




    If you're wondering what the first line does:



    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);


    This is because in OData v6 by default all query options are disabled (again, see docs). You should either enable them on each controller/action, or do as I did, and enable them globally (which is what the above line does).






    share|improve this answer















    This has changed in OData WebAPI 6. Since v6 Dependency Injection has been introduced (see docs). This means the old way of configuring your service has been changed. From now on you should register all necessary services and routes in the MapODataServiceRoute method



    public static void Register(HttpConfiguration config)
    {
    // Web API configuration and services
    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

    setErrorDetailPolicy();
    var model = BuildEdmModel();

    // Web API routes
    config.MapODataServiceRoute("routeName", "routePrefix", builder =>
    builder.AddDefaultODataServices()
    .AddService<IEdmModel>(ServiceLifetime.Singleton, s => model)
    .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => ODataRoutingConventions.CreateDefaultWithAttributeRouting("routeContact", config))
    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))
    );
    }


    The important line here is the last one:



    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model))


    After this is enabled I found that my model had issues recognizing the AddAlternateKeyAnnotation extension method. This was solved by casting the instance to an EdmModel (instead of using the IEdmModel it was). My ModelBuilder class looks something like this:



        internal static IEdmModel GetModel()
    {
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.Namespace = "MyNamespace";

    var personsSet = builder.EntitySet<PersonDTO>("Persons");
    builder.EntityType<PersonDTO>().HasKey(k => k.Id);

    var model = builder.GetEdmModel();

    // model is built, add alternateKey annotation as necessary
    // first, find entity type
    IEdmEntityType t = model.FindDeclaredEntitySet("Persons").EntityType();

    // now find the properties we want to use as alternateKey
    var firstNameProp = t.FindProperty("FirstName");
    var lastNameProp = t.FindProperty("LastName");

    // and finally add the annotation
    ((EdmModel)model).AddAlternateKeyAnnotation(t, new Dictionary<string, IEdmProperty> {
    {
    "FirstName", firstNameProp
    },
    {
    "LastName", lastNameProp
    }
    });

    return model;
    }


    Note that there are still a couple of convenience methods. From the documentation:




    You might also find that we still preserve the previous overloads of
    MapODataServiceRoute which take batch handlers, path handlers, HTTP
    message handlers, etc. They are basically wrapping the first overload
    that takes a configureAction. The reason why we keep them is that we
    want to give the users convenience to create OData services and
    bearings to the APIs they are familiar with.




    If you're wondering what the first line does:



    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);


    This is because in OData v6 by default all query options are disabled (again, see docs). You should either enable them on each controller/action, or do as I did, and enable them globally (which is what the above line does).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 6 '17 at 8:58

























    answered Mar 3 '17 at 14:08









    jonathan.meesschaertjonathan.meesschaert

    92312




    92312













    • I don't see how what you've shown above re-enables the AddAlternateKeyAnnotation functionality for models.

      – Matt Spradley
      Mar 4 '17 at 16:51











    • Note that the modelbuilding part is different from the configuration of the OData service. Your question seemed to be only about the configuration part. Anyway, adding the alternateKey annotation should work the same as it used to. See updated answer

      – jonathan.meesschaert
      Mar 6 '17 at 8:59



















    • I don't see how what you've shown above re-enables the AddAlternateKeyAnnotation functionality for models.

      – Matt Spradley
      Mar 4 '17 at 16:51











    • Note that the modelbuilding part is different from the configuration of the OData service. Your question seemed to be only about the configuration part. Anyway, adding the alternateKey annotation should work the same as it used to. See updated answer

      – jonathan.meesschaert
      Mar 6 '17 at 8:59

















    I don't see how what you've shown above re-enables the AddAlternateKeyAnnotation functionality for models.

    – Matt Spradley
    Mar 4 '17 at 16:51





    I don't see how what you've shown above re-enables the AddAlternateKeyAnnotation functionality for models.

    – Matt Spradley
    Mar 4 '17 at 16:51













    Note that the modelbuilding part is different from the configuration of the OData service. Your question seemed to be only about the configuration part. Anyway, adding the alternateKey annotation should work the same as it used to. See updated answer

    – jonathan.meesschaert
    Mar 6 '17 at 8:59





    Note that the modelbuilding part is different from the configuration of the OData service. Your question seemed to be only about the configuration part. Anyway, adding the alternateKey annotation should work the same as it used to. See updated answer

    – jonathan.meesschaert
    Mar 6 '17 at 8:59













    0














    Something missing from jonathan.meesschaert answer is the signature for your action in the OData controller.



    In my case I had the following alernate key:



    ((EdmModel)edmModel).AddAlternateKeyAnnotation(
    entityType,
    new Dictionary<string, IEdmProperty>() {
    { "Text", edmProperty }
    });


    and the asociated controller action was:



    public MenuItem Get([FromODataUri]string keyText)


    Note that you don't need to use [HttpGet] or [ODataRoute("Menus(Text={Text})")] to get it working. However, the crucial part is that I had to append key to the parameter name. Without that it didn't work on OData v7.1.0






    share|improve this answer




























      0














      Something missing from jonathan.meesschaert answer is the signature for your action in the OData controller.



      In my case I had the following alernate key:



      ((EdmModel)edmModel).AddAlternateKeyAnnotation(
      entityType,
      new Dictionary<string, IEdmProperty>() {
      { "Text", edmProperty }
      });


      and the asociated controller action was:



      public MenuItem Get([FromODataUri]string keyText)


      Note that you don't need to use [HttpGet] or [ODataRoute("Menus(Text={Text})")] to get it working. However, the crucial part is that I had to append key to the parameter name. Without that it didn't work on OData v7.1.0






      share|improve this answer


























        0












        0








        0







        Something missing from jonathan.meesschaert answer is the signature for your action in the OData controller.



        In my case I had the following alernate key:



        ((EdmModel)edmModel).AddAlternateKeyAnnotation(
        entityType,
        new Dictionary<string, IEdmProperty>() {
        { "Text", edmProperty }
        });


        and the asociated controller action was:



        public MenuItem Get([FromODataUri]string keyText)


        Note that you don't need to use [HttpGet] or [ODataRoute("Menus(Text={Text})")] to get it working. However, the crucial part is that I had to append key to the parameter name. Without that it didn't work on OData v7.1.0






        share|improve this answer













        Something missing from jonathan.meesschaert answer is the signature for your action in the OData controller.



        In my case I had the following alernate key:



        ((EdmModel)edmModel).AddAlternateKeyAnnotation(
        entityType,
        new Dictionary<string, IEdmProperty>() {
        { "Text", edmProperty }
        });


        and the asociated controller action was:



        public MenuItem Get([FromODataUri]string keyText)


        Note that you don't need to use [HttpGet] or [ODataRoute("Menus(Text={Text})")] to get it working. However, the crucial part is that I had to append key to the parameter name. Without that it didn't work on OData v7.1.0







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 15:18









        Manu A.B.Manu A.B.

        112




        112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41929367%2fhow-to-set-enable-alternate-keys-with-microsoft-aspnet-odata-version-6%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Futebolista

            Jornalista