ADO.NET 实体数据模型序列化问题
ADO.NET Entity Data Model serialization issues
我对这个框架很陌生。我让 VS 从我现有的数据库中搭建一个数据模型,但我在尝试从其 API 2 控制器正确获取数据时遇到了问题。
这是模型:
namespace AngularWebApi.Models
{
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
public partial class BlockDeveloper
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BlockDeveloper()
{
this.Reviews = new HashSet<Review>();
this.Topics = new HashSet<Topic>();
}
public int DeveloperId { get; set; }
public string UserName { get; set; }
public string PassWord { get; set; }
public string eMail { get; set; }
int PostCount { get; set; }
public int RespondCount { get; set; }
public int Score { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Review> Reviews { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Topic> Topics { get; set; }
}
}
这是我要执行的 GET:
namespace AngularWebApi.Controllers
{
public class BlockDevelopersController : ApiController
{
private AngularSiteEntities4 db = new AngularSiteEntities4();
// GET: api/BlockDevelopers
public IQueryable<BlockDeveloper> GetBlockDevelopers()
{
return db.BlockDevelopers;
}
//....more actions...
}
}
我一直收到以下错误:
Type 'System.Data.Entity.DynamicProxies.BlockDeveloper_2BE89EC23AFBD5F46CBA6ED3403E8895016357CD4A3553D07BA526B43E774273' with data contract name 'BlockDeveloper_2BE89EC23AFBD5F46CBA6ED3403E8895016357CD4A3553D07BA526B43E774273:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
我阅读了很多帖子和论坛,弹出了两个关键解决方案:使用 [KnownType(typeof(T))] 和 Configuration.ProxyCreationEnabled = false;.
我在我的代码中到处都使用了 KnownType 属性,但运气不好 - 我完全不知道在自动生成的 ADO.NET 实体数据模型中在哪里执行此操作。
我终于在我的 BlockDevelopersController 中尝试 "Configuration.ProxyCreationEnabled = false;",我不再收到错误。我终于可以看到我的 BlockDeveloper 列表了——但有一个问题,none 的子对象(评论、主题)正在显示(它们在序列化时是空标签)。我知道数据存在,因为如果我调试并单独循环遍历每个 BlockDeveloper 的评论和主题,我就可以看到它。
我完全不知道如何获取我的所有数据。如果我使用 "Configuration.ProxyCreationEnabled = false;",有没有办法获得它?我似乎在读我不能。我如何使用“[KnownType(typeof(T))]”属性来更正此问题(这些属性在 ADO.NET 实体数据模型项目中的确切位置?)。
任何帮助都会很棒,谢谢!
当您尝试将它们序列化为 JSON 时,模型中的虚拟属性会导致无限循环,因为 Review
和 Topic
引用了 BlockDeveloper
,反之亦然反之亦然。您应该使用预加载从数据库中获取 Review
和 Topic
模型,而不是尝试延迟加载它们。
我对这个框架很陌生。我让 VS 从我现有的数据库中搭建一个数据模型,但我在尝试从其 API 2 控制器正确获取数据时遇到了问题。
这是模型:
namespace AngularWebApi.Models
{
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
public partial class BlockDeveloper
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BlockDeveloper()
{
this.Reviews = new HashSet<Review>();
this.Topics = new HashSet<Topic>();
}
public int DeveloperId { get; set; }
public string UserName { get; set; }
public string PassWord { get; set; }
public string eMail { get; set; }
int PostCount { get; set; }
public int RespondCount { get; set; }
public int Score { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Review> Reviews { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Topic> Topics { get; set; }
}
}
这是我要执行的 GET:
namespace AngularWebApi.Controllers
{
public class BlockDevelopersController : ApiController
{
private AngularSiteEntities4 db = new AngularSiteEntities4();
// GET: api/BlockDevelopers
public IQueryable<BlockDeveloper> GetBlockDevelopers()
{
return db.BlockDevelopers;
}
//....more actions...
}
}
我一直收到以下错误:
Type 'System.Data.Entity.DynamicProxies.BlockDeveloper_2BE89EC23AFBD5F46CBA6ED3403E8895016357CD4A3553D07BA526B43E774273' with data contract name 'BlockDeveloper_2BE89EC23AFBD5F46CBA6ED3403E8895016357CD4A3553D07BA526B43E774273:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
我阅读了很多帖子和论坛,弹出了两个关键解决方案:使用 [KnownType(typeof(T))] 和 Configuration.ProxyCreationEnabled = false;.
我在我的代码中到处都使用了 KnownType 属性,但运气不好 - 我完全不知道在自动生成的 ADO.NET 实体数据模型中在哪里执行此操作。
我终于在我的 BlockDevelopersController 中尝试 "Configuration.ProxyCreationEnabled = false;",我不再收到错误。我终于可以看到我的 BlockDeveloper 列表了——但有一个问题,none 的子对象(评论、主题)正在显示(它们在序列化时是空标签)。我知道数据存在,因为如果我调试并单独循环遍历每个 BlockDeveloper 的评论和主题,我就可以看到它。
我完全不知道如何获取我的所有数据。如果我使用 "Configuration.ProxyCreationEnabled = false;",有没有办法获得它?我似乎在读我不能。我如何使用“[KnownType(typeof(T))]”属性来更正此问题(这些属性在 ADO.NET 实体数据模型项目中的确切位置?)。
任何帮助都会很棒,谢谢!
当您尝试将它们序列化为 JSON 时,模型中的虚拟属性会导致无限循环,因为 Review
和 Topic
引用了 BlockDeveloper
,反之亦然反之亦然。您应该使用预加载从数据库中获取 Review
和 Topic
模型,而不是尝试延迟加载它们。