C# WebAPI W OData&;EF:导航属性错误

C# WebAPI W OData&;EF:导航属性错误,c#,angularjs,entity-framework,asp.net-web-api,odata,C#,Angularjs,Entity Framework,Asp.net Web Api,Odata,早上好 我已经在DB SQLServer上创建了一个带有EF的WebApi。我还使用了OData vers 4.0。 我需要扩展模型以创建数据库中不存在的关系 我有一个实体时钟和另一个接口(1个接口-N个时钟) 我以以下方式扩展了模型: public partial class Clock { /* OData needs this field, I don't want to modify EF mapping autogenerated files */ [

早上好

我已经在DB SQLServer上创建了一个带有EF的WebApi。我还使用了OData vers 4.0。 我需要扩展模型以创建数据库中不存在的关系

  • 我有一个实体时钟和另一个接口(1个接口-N个时钟)
  • 我以以下方式扩展了模型:

    public partial class Clock
    {
        /*
        OData needs this field, I don't want to modify EF mapping autogenerated files
        */
        [Key]
        public int ID
        {
            get { return this.ClockID; }
            set { this.ClockID = value; }
        }
        public Interface
        {
            get
            {
                using (EF.DBClocksContainer db = new EF.DBClocksContainer())
                {
                    return db.tbl_Interface.Where(x => x.ID == this.InterfaceID).SingleOrDefault();
                }
            }
            set { throw new NotImplementedException(); }
        }
    
我使用OData4脚手架创建了一个新的控制器:然后在EF的模型和CRUD方法的时钟实体上有一个带有OData4的控制器

返回IQueryable的GET方法很简单。只需应用odata查询:

        [EnableQuery]
        public IQueryable<Clock> GetClocks(ODataQueryOptions<Clock> queryOptions)
        {
            return db.tbl_Clock;
        }
我收到一级属性=>而不是“接口”字段的时钟列表。对于odata的“导航属性”,我们必须使用$expand comand:

http://localhost:51432/odata/Clocks?$expand=Interface
现在我收到以下错误:

[…]
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.",
[…]
The specified type member 'Interface' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.",
[…]
然而,最奇怪的是:如果我使用$expand方法执行单个元素GET,则所有元素都能正常工作:

http://localhost:51432/odata/Clocks(2)?$expand=Interface
你有什么想法吗

谢谢。尼古拉


我通过在EF模型中添加关联来解决这个问题。Nicola。

我不能用几乎相同的代码重新编程,你能不能在上创建问题并提供重新编程细节,如自动生成的模型类,我会为你调查。我不能用几乎相同的代码重新编程,你能不能在上创建问题并提供重新编程细节,如自动生成的模型类,我会为你调查。
[…]
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.",
[…]
The specified type member 'Interface' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.",
[…]
http://localhost:51432/odata/Clocks(2)?$expand=Interface