带参数的Odata函数抛出500



我的一个控制器有点问题。

每次我试图用参数调用某个函数时,都会抛出500,在调试时,我可以看到该函数甚至没有被调用。

首先我的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;
        }
    }

我的实体:

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

和我的控制器:

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

……}

第一个Get方法运行良好。

当涉及到第二个Get with a Parameter时,我会得到一个内部服务器错误。

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

谁能给我一个提示,可能出了什么问题?

问候Andre

带有键的方法应该返回ObjectValue而不是IQueryable<ObjectValue>,并且key参数错误,它与ObjectValue对象上的键不匹配。你的意思是在ObjectId上有Key属性,在ObjectValue上有Attribute属性吗?如果是这样,您需要在GetObjectValues方法上有两个名称与键匹配的键参数,否则请删除其中一个Key属性,并确保key参数的类型与ObjectValue上键的类型匹配。

我不知道我在配置中到底做错了什么,但这解决了我的问题:

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

问候Andre

最新更新