/ / OData webapi2 ne peut pas modifier un compte à un autre modèle - asp.net-web-api, odata

OData webapi2 ne peut pas changer d'un compte à l'autre ViewViewModel - asp.net-web-api, odata

J'ai le suivant

        [EnableQuery]
public IQueryable<AccountViewModel> GetAccounts()
{
return _accountRepository.Table
.Select(x => new Bepoz.Api.Models.AccountViewModel() {Title = x.Title, AccountID = x.AccountID}).AsQueryable();
}

Et

    [EnableQuery]
public SingleResult<AccountViewModel> GetAccount([FromODataUri] int key)
{
return SingleResult.Create(this._accountRepository.Table.Where(account => account.AccountID == key)
.Select(x => new Bepoz.Api.Models.AccountViewModel() { Title = x.Title, AccountID = x.AccountID }).AsQueryable());
}

Dans mon WebApiConfig

        public static void Register(HttpConfiguration config)
{
// New code:
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<AccountViewModel>("Accounts");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: builder.GetEdmModel());
}

Quand j'exécute

http://localhost:55045/odata/Accounts

La réponse correspond à tous les comptes attendus, mais lorsque j'exécute

http://localhost:55045/odata/Accounts(1)

Je reçois

{
"error":{
"code":"","message":"No HTTP resource was found that matches the request URI "http://localhost:55045/odata/Accounts(1)".","innererror":{
"message":"No routing convention was found to select an action for the OData path with template "~/entityset/key".","type":"","stacktrace":""
}
}
}

Mon AccountViewModel ressemble à ceci

public class AccountViewModel
{
[Key]
public int AccountID { get; set; }
public string Title { get; set; }
}

FYI - Si je change la classe de AccountViewModel à Account, cela fonctionnera! Merci

Réponses:

1 pour la réponse № 1

La seule chose que vous devez faire est d'ajouter 2 lignes supplémentaires:

ODataModelBuilder builder = new ODataConventionModelBuilder();
EntitySetConfiguration<AccountViewModel> accounts= builder.EntitySet<AccountViewModel>("Accounts");
EntityTypeConfiguration<AccountViewModel> account = accounts.EntityType;
account.Name = "Account";