locked
Error in exposing Pocos in Data services using EF 4 RRS feed

  • Question

  • Hi,

    I tried using Pocos with EF (as per this article - http://blogs.msdn.com/b/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx) and tried to expose it using WCF Data services. Note, the edmx file and context are in 1 assembly,  the Pocos are in separate assembly and service in a separate web project. I got the error below and looked for a solution 

    I found this thread - http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/601491ab-9c46-4fdf-88a7-c7392e5dbcdf.

    and tried implementing the solution and added to the service.cs class, but still the same error. 

     

     

    protected override MycontainerContext CreateDataSource()<br/>  {
       MycontainerContext context = new MycontainerContext ();<br/>
       context.MetadataWorkspace.LoadFromAssembly(typeof(MycontainerContext).Assembly);<br/>
       return context;
      }
    

     

     

     

    The server encountered an error processing the request. The exception message is 'Value cannot be null. Parameter name: key'. See server logs for more details. The exception stack trace is:

    at System.Collections.Generic.Dictionary`2.FindEntry(TKey key) at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 childTypes, IDictionary`2 entitySets) at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata() at System.Data.Services.DataService`1.CreateProvider() at System.Data.Services.DataService`1.EnsureProviderAndConfigForRequest() at System.Data.Services.DataService`1.HandleRequest() at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody) at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

    I created a separate  test project and it works fine -

     

    TestClass]

        public class MyContainerDatabaseTests

        {

            private MycontainerContext context;


            [TestInitialize]

            public void Start()

            {

                context = new MycontainerContext ();

            }


            [TestCleanup]

            public void Finish()

            {

                context.Dispose();

            }


            [TestMethod]

            public void QueryForCountries()

            {

                var countries = (from c in context.Countries

                                  select c);

                Assert.IsTrue(countries.Count() > 0);

            }

        }

     

     


    Tuesday, May 10, 2011 7:04 PM

All replies

  • ok So I debugged it and I can clearly see modelcontainer correctly loads all objects.objectset. But the service still throws this error. 

    Someone Please advise.

    Tuesday, May 10, 2011 10:17 PM
  • Hello,

    You need to add another call to MetadataWorkspace.LoadFromAsembly with the assembly containg your POCO classes.

     

    Hope this helps!


    Monica Frintu
    Wednesday, May 11, 2011 9:15 PM
    Moderator
  • Hello,

    You need to add another call to MetadataWorkspace.LoadFromAsembly with the assembly containg your POCO classes.

     

    Hope this helps!


    Monica Frintu

    Hi Monica,

    How can I modify this to add another Loadfromassembly call for Pocos? The CreateDataSource returns the Context class. Please find my code below. Thanks

    protected override myContext CreateDataSource()

            {

                using (var dbConnection = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection())

                {

                    dbConnection.ConnectionString =

                        "Data Source=server;Initial Catalog=tablename;Integrated Security=True;MultipleActiveResultSets=True";

                    var metaData = new MetadataWorkspace(new string[] { "res://*/" }, new Assembly[] { typeof(myContext).Assembly });

                    using (var entityConnection = new EntityConnection(metaData, dbConnection))

                    {

                        var modelContainer = new myContext();

                        modelContainer.MetadataWorkspace.LoadFromAssembly(typeof(myContext).Assembly);

                        return modelContainer;

                    }

                }

            }

    Wednesday, May 11, 2011 9:36 PM
  • Change your code to something like this

     

    protected override MycontainerContext CreateDataSource(){
     MycontainerContext context = new MycontainerContext ();
     context.MetadataWorkspace.LoadFromAssembly(typeof(MycontainerContext).Assembly);
     context.MetadataWorkspace.LoadFromAssembly(typeof(APocoType).Assembly); 
     return context;
     }
    
    

    Where "APocoType" is the actual name of one of your poco types.

    -jeff 


    Wednesday, May 11, 2011 9:43 PM
  • Change your code to something like this

     

    protected override MycontainerContext CreateDataSource(){
     MycontainerContext context = new MycontainerContext ();
     context.MetadataWorkspace.LoadFromAssembly(typeof(MycontainerContext).Assembly);
     context.MetadataWorkspace.LoadFromAssembly(typeof(APocoType).Assembly); 
     return context;
     }
    
    

    Where "APocoType" is the actual name of one of your poco types.

    -jeff 


    thanks. I changed it but still getting the same error....

    Thursday, May 12, 2011 7:02 PM
  • Can you debug and see if your types are in the MetadataWorkspace  ObjectItemCollection after the second LoadFromAssembly call?  Also, you may want to try the LoadFromAssembly overload that gives you messages about types that were considered but didn't match.

     

    -jeff


    Friday, May 13, 2011 9:48 PM