我目前正在开发一个 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);
}
}
我正在调用我在路由中设置的以下 url:http://127.0.0.1:81/odata/Users
如果我在我的用户模型中评论导航属性,它会起作用。
我究竟做错了什么?我已经尝试将 [Serializable, KnownType(typeof(Language))] 放在我的用户类之上,但由于某种原因我不能使用 KnownType。如何使我的 OData 与此导航属性一起使用?