【问题标题】:WCF Service not returning virtual property ServiceProviderWCF 服务未返回虚拟属性 ServiceProvider
【发布时间】:2015-06-09 18:14:04
【问题描述】:
public class Account
{
    [DataMember]
    public int AccountId { get; set; }
    [DataMember]
    public string Email { get; set; }
    [DataMember]
    public string Password { get; set; }
    [DataMember]
    public string ConfirmPassword { get; set; }

    [ForeignKey("ServiceProvider")]
    [DataMember]
    public int ServiceProviderId { get; set; }
    [DataMember]
    public virtual ServiceProvider ServiceProvider { get; set; }
}

尝试使用时

this.context.Configuration.LazyLoadingEnabled = false;
this.context.Configuration.ProxyCreationEnabled = false;

它以null返回ServiceProvider

【问题讨论】:

  • this.context.Configuration.LazyLoadingEnabled = true 时返回错误; // this.context.Configuration.ProxyCreationEnabled = true;底层连接已关闭:接收时发生意外错误。
  • 也在这里发布您的ServiceProvider 实体。
  • 如果您将 EntityFramework 暴露给 WCF,您应该考虑使用 WCF DataServices,它专门用于处理您遇到的问题。
  • [DataContract] public class ServiceProvider { [DataMember] [Key, Column("ServiceProviderId", Order = 0)] public int ServiceProviderId { get;放; } [DataMember] 公共 int Id { 获取;放; } [DataMember] 公共字符串名称 { 获取;放; } [DataMember] 公共字符串 ContactPersonName1 { 获取;放; } [DataMember] 公共字符串 ContactPersonName2 { 获取;放; } }

标签: c# .net entity-framework wcf


【解决方案1】:

使用包含方法的预加载:

using System.Data.Entity;
//...
context.Accounts.Include(x => x.ServiceProvider).Where(...)

请参阅此主题以获得说明: What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first

【讨论】:

  • 我的 db 数据已成功发送到服务方法,但无法发送给客户端
  • 您可能会遇到序列化问题、客户端\服务器配置错误或客户端模型问题。确保 [DataContract] 在任何地方设置。检查没有提供者的帐户是否工作。检查客户端模型(帐户有提供者成员)。
  • 是的,它在没有提供程序的情况下工作 fyn,我知道它是一个序列化问题,但是是什么问题 :-) 我已经正确使用了 DataContract 和 DataMember,当我使用延迟加载 =false 时,serviceprovider 为空但它到达客户端成功
  • 好的,首先你需要 ProxyCreationEnabled = false 因为你从上下文中得到的代理不能被序列化。其次,在 ProxyCreationEnabled = false 的情况下,您需要使用单独的查询加载 ServiceProvider,或者您对帐户的初始查询应包含加载 ServiceProvider 的直接指令,这是通过我在答案中编写的 Include 方法完成的。您可以先选择具有不同查询的提供者并将其分配给帐户,以确保具有不为空提供者和 ProxyCreationEnabled =false 的帐户成功序列化。
猜你喜欢
  • 1970-01-01
  • 2014-09-21
  • 2013-01-17
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多