错误:
<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,ODataSerializerContext writeContext) at System.Web.http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObjectInline(Object graph,ODataSerializerContext writeContext) at System.Web.http.OData.Formatter.Serialization.OdatafeedSerializer.WriteFeed(Object graph,ODataSerializerContext writeContext) at System.Web.http.OData.Formatter.Serialization.OdatafeedSerializer.WriteObjectInline(Object graph,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>(); }}
而我的UsersController for 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/如果我在我的用户模型中注释了导航属性,则可以使用它.
我究竟做错了什么?我已经尝试将[Serializable,KNownType(typeof(Language))]放在我的用户类之上,但由于某种原因我不能使用KNownType.我如何使我的OData与此导航属性一起使用?
解决方法 您缺少为语言设置的实体.导航属性指向实体,必须绑定到实体集.在您的模型构建器代码中,添加此行来解决问题.builder.EntitySet<Language>("languages");
这样做是创建实体集“语言”.约定模型构建器将导航属性绑定到实体集合(如果有的话).因此,导航属性实体类型用户的语言将被绑定到实体集语言.
总结以上是内存溢出为你收集整理的c# – Odata No NavigationLink工厂被发现全部内容,希望文章能够帮你解决c# – Odata No NavigationLink工厂被发现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)