1
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; }
}

When tried with

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

it return ServiceProvider as null

DeanOC
  • 7,142
  • 6
  • 42
  • 56
  • It returns error when this.context.Configuration.LazyLoadingEnabled = true; // this.context.Configuration.ProxyCreationEnabled = true;The underlying connection was closed: An unexpected error occurred on a receive. – user3906377 Jun 09 '15 at 18:18
  • Post your `ServiceProvider` entity here too. – Alexandre Severino Jun 09 '15 at 18:19
  • If you are exposing EntityFramework to WCF you should look in to using [WCF DataServices](https://msdn.microsoft.com/en-us/library/dd673932(v=vs.110).aspx), it is specifically built to handle issues like the one you ran in to. – Scott Chamberlain Jun 09 '15 at 21:53
  • [DataContract] public class ServiceProvider { [DataMember] [Key, Column("ServiceProviderId", Order = 0)] public int ServiceProviderId { get; set; } [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string ContactPersonName1 { get; set; } [DataMember] public string ContactPersonName2 { get; set; } } – user3906377 Jun 11 '15 at 19:41

1 Answers1

1

Use eager loading with Include method:

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

See this topic for clarification: What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first

Community
  • 1
  • 1
FireAlkazar
  • 1,795
  • 1
  • 14
  • 27
  • My Data from db is coming successfully to service method but not able to send it to client – user3906377 Jun 11 '15 at 19:39
  • You can have serializations issues, misconficuration of client\server or client side model problems. Make sure [DataContract] is set everywhere. Check if Account without Provider work or not. Check client side model(Account has Provider member). – FireAlkazar Jun 11 '15 at 22:03
  • Yes its working fyn without provider and i know its a serialization issue but what issue :-) i have used DataContract and DataMember properly and when i use lazy loading =false, serviceprovider is null but it reaches client successfully – user3906377 Jun 12 '15 at 02:29
  • Ok, first you need ProxyCreationEnabled = false as proxies you get from context can't be serialized. Second, in case of ProxyCreationEnabled = false you need to load ServiceProvider with separate query or your initial query for accounts should contain a direct instruction to load ServiceProvider and this is done by Include method i wrote in the answer. You can start with selecting provider with distinct query and assigning it to account to make sure that Account with not null Provider and ProxyCreationEnabled =false serialized succesfully. – FireAlkazar Jun 12 '15 at 11:12