Odata找不到NavigationLink工厂 [英] Odata No NavigationLink factory was found

查看:97
本文介绍了Odata找不到NavigationLink工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用mvc4 Web API Odata服务,我想返回用户列表,其中用户具有语言列表.当我想获取用户时,出现以下错误:

I am currently working on a mvc4 web api odata service where I want to return a List of Users where Users have a list of Languages. When I want to get the Users I get the following error:

错误:

<m:innererror>
<m:message>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.
</m:message>
<m:type>System.InvalidOperationException</m:type>
<m:stacktrace/>
<m:internalexception>
<m:message>
No NavigationLink factory was found for the navigation property 'Languages' from entity type 'MvcWebRole1.Models.User' on entity set 'Users'. Try calling HasNavigationPropertyLink on the EntitySetConfiguration.
 Parameter name: navigationProperty
</m:message>
<m:type>System.ArgumentException</m:type>
<m:stacktrace>
at System.Web.Http.OData.Builder.EntitySetLinkBuilderAnnotation.BuildNavigationLink(EntityInstanceContext instanceContext, IEdmNavigationProperty navigationProperty, ODataMetadataLevel metadataLevel)
 at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteNavigationLinks(EntityInstanceContext context, ODataWriter writer, ODataSerializerContext writeContext)
 at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteEntry(Object graph, IEnumerable`1 propertyBag, ODataWriter writer, ODataSerializerContext writeContext)
 at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObjectInline(Object graph, ODataWriter writer, ODataSerializerContext writeContext)
 at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(Object graph, ODataWriter writer, ODataSerializerContext writeContext)
 at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObjectInline(Object graph, ODataWriter writer, ODataSerializerContext writeContext)
 at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
 at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.<>c__DisplayClassa.<WriteToStreamAsync>b__9()
 at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
</m:stacktrace>
</m:internalexception>
</m:innererror>

我的用户如下:

public class User
{
    [Key]
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }

    public string Password { get; set; }
    public virtual ICollection<Language> Languages { get; set; }

    public User()
    {
        Languages = new List<Language>();
    }
}

,我的OData用户控制器如下所示:

public class UsersController : EntitySetController<User, int>
{
    WorldChatContext db = new WorldChatContext();

    public override IQueryable<User> Get()
    {
        return db.Users.AsQueryable();
    }

    protected override User GetEntityByKey(int key)
    {
        return db.Users.FirstOrDefault(p => p.UserId == key);
    }

    public override HttpResponseMessage Post(User entity)
    {
        db.Users.Add(entity);
        db.SaveChanges();
        return base.Post(entity);
    }
}

我正在调用在路由中设置的以下网址:http://127.0.0.1:81/odata/Users如果我在用户模型中评论导航属性,它将起作用.

I am calling the following url which i setup in my routing: http://127.0.0.1:81/odata/Users it works if I comment navigation property in my User model.

我在做什么错?我已经尝试将[Serializable,KnownType(typeof(Language))]放在我的用户类之上,但是由于某些原因,我无法使用KnownType.如何使我的OData与此导航属性配合使用?

What am i doing wrong? I already tryed to put [Serializable, KnownType(typeof(Language))] on top of my user class but for some reason i cant use KnownType. How can i make my OData work with this navigation property?

推荐答案

您缺少为语言"设置的实体.导航属性指向实体,并且必须绑定到实体集.在模型构建器代码中,添加以下行以解决此问题.

You are missing the entity set for Language. A navigation property points to an entity and has to be bound to an entity set. In your model builder code, add this line to fix the issue.

builder.EntitySet<Language>("languages");

这是创建实体集语言".约定模型构建器将导航属性绑定到实体集(如果有).因此,实体类型User上的导航属性Languages将绑定到实体集languages.

What this does is to create the entity set 'languages'. The convention model builder binds a navigation property to an entity set if there is one. So, the navigation property Languages on the entity type User will be bound to the entity set languages.

这篇关于Odata找不到NavigationLink工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆