0
votes

i have a little Problem with one of my Controllers.

Everytime i try to call some function with parameter a 500 is thrown and while debugging i can see that the function is not even called.

First my WebApiConfig:

public static class WebApiConfig
{
        public static void Register(HttpConfiguration config)
        {
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Entity>("Entities");
            builder.EntitySet<DataTypes>("DataTypes");
            builder.EntitySet<ObjectValue>("ObjectValues");
            builder.EntitySet<Attributes>("Attributes");
            builder.EntitySet<Objects>("Objects");
            builder.Namespace = "EAVService.Controllers";
            builder.Action("FullAttributes").Returns<IHttpActionResult>()
                .CollectionParameter<Attributes>("Attributes");
            builder.Action("FullValues").Returns<IHttpActionResult>()
                .CollectionParameter<ObjectValue>("ObjectValue");

            config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: "odata",
                model: builder.GetEdmModel());
            config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
        }
    }

My Entity:

[Table("ObjectValue")]
public partial class ObjectValue
{
    public ObjectValue()
    {


    }
    [Key]
    [Column(Order = 0)]
    public int ObjectId { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(50)]
    public string Attribute { get; set; }

    [StringLength(256)]
    public string AttributeVal { get; set; }

    public virtual Attributes Attributes { get; set; }

    public virtual Objects Objects { get; set; }
}

and my Controller:

public class ObjectValuesController : ODataController
{
    private EAVModel db;

    public ObjectValuesController(IDbConnectionProvider provider)
    {
        db = new EAVModel(provider.GetDbConnection());
    }

    // GET: odata/ObjectValues
    [EnableQuery]
    public IQueryable<ObjectValue> GetObjectValues()
    {
        IQueryable<ObjectValue> query = db.ObjectValue.AsQueryable();
        return query;
    }

    // GET: odata/ObjectValues(5)
    [EnableQuery]
    public IQueryable<ObjectValue> GetObjectValues([FromODataUri] string key)
    {
        IQueryable<ObjectValue> result = db.ObjectValue.Where(objectValue => objectValue.ObjectId == Convert.ToInt32(key)).AsQueryable();

        return result;
    }

.... }

The first Get Method is working fine.

When it comes to the second Get with a Parameter i get an Internal Server Error.

http://localhost:80/EAVServiceAPI/odata/ObjectValues(1)

Someone who can give me a hint what could be wrong?

Regards Andre

2

2 Answers

0
votes

The method with the key should return ObjectValue not IQueryable<ObjectValue> and the key parameter is wrong, it doesn't match the key on the ObjectValue object. Do you mean to have Key attributes on ObjectId and Attribute on ObjectValue? If so you need to have 2 key parameters on your GetObjectValues method with names that match the keys, otherwise remove one of the Key attributes and ensure the type of your key parameter matches the type of the key on ObjectValue.

0
votes

I dont know what exactly i did wrong in my configuration but this solved my issue:

http://localhost/EAVServiceAPI/odata/ObjectValues?objectId=1

Regards Andre