I am using Entity Framework 6.2.0 with my WCF service hosted in IIS. I am getting this error when trying to eager load related table data. I have two tables Product and Category joined by CategoryID.
Trying to fetch products data and their related category Name but getting that error (I have tried almost every solution on web from last 24 hours).
public List<Product> GetAllProducts(int start, int end) { return _context.Products.Include(x=>x.Category).ToList(); } I have also set following properties to false.
public Intelliventory_DBEntities() : base("name=Intelliventory_DBEntities") { this.Configuration.ProxyCreationEnabled = false; this.Configuration.LazyLoadingEnabled = false; } App.config
<configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IIntelliventoryService" closeTimeout="10:01:00" openTimeout="10:01:00" receiveTimeout="10:10:00" sendTimeout="10:01:00" allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> </binding> </basicHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="ServiceBehavior"> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </endpointBehaviors> </behaviors> <client> <endpoint name="BasicHttpBinding_IIntelliventoryService" address="http://localhost:5050/IntelliventoryService.svc" behaviorConfiguration="ServiceBehavior" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IIntelliventoryService" contract="IntelliventoryService.IIntelliventoryService" /> </client> </system.serviceModel> </configuration> Web.Config
<configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.8" /> <httpRuntime targetFramework="4.8" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IIntelliventoryService" closeTimeout="10:01:00" openTimeout="10:01:00" receiveTimeout="10:10:00" sendTimeout="10:01:00" allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" useDefaultWebProxy="true"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior> <dataContractSerializer maxItemsInObjectGraph ="2147483647"/> <serviceThrottling maxConcurrentCalls="16" maxConcurrentInstances="26" maxConcurrentSessions="10" /> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <directoryBrowse enabled="true" /> </system.webServer> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> <connectionStrings> <add name="Intelliventory_DBEntities" connectionString="metadata=res://*/ADOModel.csdl|res://*/ADOModel.ssdl|res://*/ADOModel.msl;provider=System.Data.SqlClient; provider connection string="data source=RAO-HAMMAS-PC;initial catalog=Intelliventory_DB;persist security info=True; user id=admin;password=admin;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings></configuration> I don't get this error when i don't eager load category table.
Also if I try to load just specific column, I get this error.
_context.Products.Include(x=>x.Category.CategoryName).ToList(); A specified Include path is not valid. The EntityType 'Intelliventory_DBModel.Category' does not declare a navigation
Category Table from Db Context
public int CategoryID { get; set; } public string CategoryName { get; set; } Product Table from Db Context
public partial class Product { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Product() { this.Purchases = new HashSet<Purchase>(); } public int ProductID { get; set; } public string ProductName { get; set; } public int CategoryID { get; set; } public System.DateTime Date { get; set; } public double Quantity { get; set; } public string Model { get; set; } public string Details { get; set; } public double SellPrice { get; set; } public double PurchasePrice { get; set; } public int SupplierID { get; set; } public Nullable<double> PaidAmount { get; set; } public Nullable<int> NoOfSales { get; set; } public Nullable<double> DueAmountToPay { get; set; } public Nullable<System.DateTime> ExpiryDate { get; set; } public virtual Category Category { get; set; } public virtual Supplier Supplier { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Purchase> Purchases { get; set; } } As @Andy pointed that Category is not ICollection also Supplier is not ICollection but Purchases is ICollection. But it was all auto generated. Did i do something wrong ?
This is what happens during data fetch event (SSMS Live Event)
Update: I created a console app and directly used EF and it worked
But again when i used WCF in this console app to fetch Products, i got same error !
StackTrace of Exception
System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at Intelliventory.IntelliventoryService.GetAllProductsCompletedEventArgs.get_Result() in E:\\2- Development\\2-NATIVE WORLD\\Projects\\Intelliventory\\Intelliventory\\Connected Services\\IntelliventoryService\\Reference.cs:line 321 at Intelliventory.UserControls.ProductControl.ClientOnGetAllProductsCompleted(Object sender, GetAllProductsCompletedEventArgs e) in E:\\2- Development\\2-NATIVE WORLD\\Projects\\Intelliventory\\Intelliventory\\UserControls\\ProductControl.xaml.cs:line 89 at Intelliventory.IntelliventoryService.IntelliventoryServiceClient.OnGetAllProductsCompleted(Object state) in E:\\2- Development\\2-NATIVE WORLD\\Projects\\Intelliventory\\Intelliventory\\Connected Services\\IntelliventoryService\\Reference.cs:line 1082 at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at Intelliventory.App.Main() All three Exceptions !
Update: i have got the exception details (Category contains Cycles)
'IntelliventoryWcfService.Category' contains cycles and cannot be serialized if reference tracking is disabled







x.Category.Category? Seeing your model it shouldn't even compile.