我目前正在开发mvc4 web api odata服务,我希望返回用户列表中的用户列表。当我想要获得用户时,我收到以下错误:
错误:
<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
如果我在我的用户模型中评论导航属性,它会有效。
我做错了什么?我已经尝试将[Serializable,KnownType(typeof(Language))]放在我的用户类之上但由于某种原因我不能使用KnownType。如何使用此导航属性使我的OData工作?
答案 0 :(得分:32)
您缺少为语言设置的实体。导航属性指向实体,必须绑定到实体集。在模型构建器代码中,添加此行以解决问题。
builder.EntitySet<Language>("languages");
这样做是为了创建实体集'languages'。约定模型构建器将导航属性绑定到实体集(如果有)。因此,实体类型Languages
上的导航属性User
将绑定到实体集languages
。