entity framework - OData service does not return custom objects -


here want do:

  1. i have existing database need data.

  2. i created data contracts map them respective tables using entity framework. in example, have 3 tables - tblorder, tblproduct , tblcustomer. created 3 data contracts map these tables. entity framework annotations added data contratcs. wrote unit test test out data contracts. works expected direct entity framework calls. see unit test 1 below.

  3. added dataservicekey annotation each data contract. , wrap them odata service adding order context , data service svc. see below.

  4. wrote unit test access existing order data via odata sevice. problem: odata service returns non-custom data types on order object. returns null on custome data type fields, such customerinfo , productlist.

am doing wrong? there special have odata calls work on objects using ef retrieval?


[dataservicekey("id")]       [table("tblorder", schema = "schonlinesale")]       public class order      {            [column("orderid"), key]              public int id { get; set; }       public string ordername { get; set; }       public int customerid { get; set; }       [foreignkey("customerid")]      public virtual customer customerinfo { get; set; }       [foreignkey("orderid")]      public virtual icollection<product> productlist{ get; set; } }  [dataservicekey("customerid")]      [table("tblcustomer", schema = "schonlinesale")]      public class customer     {       [key]      public int customerid{ get; set; }       [column("fullname")]      public string name { get; set; }       public string mailingaddress { get; set; }         public string city { get; set; }         public string state { get; set; }         public string zipcode { get; set; }    }  [dataservicekey("productid")]    [table("tblproduct", schema = "schonlinesale")]     public class product     {       [key]      public int productid{ get; set; }       public int orderid{ get; set; }       public string productname { get; set; }       public decimal saleprice { get; set; }    } 

//odata service wrapper classes     public class ordercontext:  dbcontext     {               public dbset<order> orders {get; set;}       public dbset<product> products {get; set;} }  [servicebehavior(includeexceptiondetailinfaults = true)]    public class orderdataservice : dataservice<ordercontext>     {          // method called once initialize service-wide policies.         public static void initializeservice(dataserviceconfiguration config)         {             config.setentitysetaccessrule("orders", entitysetrights.all);             config.setentitysetaccessrule("products", entitysetrights.all);             config.dataservicebehavior.maxprotocolversion = dataserviceprotocolversion.v2;             config.useverboseerrors = true;         }          protected override void handleexception(handleexceptionargs args)         {             try             {                 args.useverboseerrors = true;             }             catch (exception ex)             {                 console.writeline(ex.message);             }         }          protected override ordercontext createdatasource()         {             var datasource = new ordercontext();             datasource.configuration.proxycreationenabled = false;             return datasource;                     }     } 

//unit test 1 - querying via data context directly public void efdatacontext_getordertest() {                 var context = new ordercontext();                 var list = (from in context.orders            a.orderid == 10 ||                  a.orderid == 15 ||                 a.orderid== 20 select a).tolist();      //customerinfo , productlist values populated }   //unit test 2 - querying via odata service public void odata_getordertest() {   string uristring = "http://localhost/orderservices/orderdataservice.svc/orders(10)";   uri serviceuri = new uri(uristring);   ordercontext context = new ordercontext(serviceuri);   context.ignoreresourcenotfoundexception = true;   var orderlist = context.orders;                foreach (order s in orderlist)   {      testcontext.writeline("orderid=" + s.id);       assert.isnotnull(s.customerinfo);  //customerinfo not returned odata call                                   //but it's returned entity framework call.       foreach (product p in s.productlist)      {         //productlist not returned odata service call,         //but returned when accessing through entity framework calls              //directly      }     }             } 

//app.config    <configuration>       <connectionstrings>         <add name="ordercontext" connectionstring="a valid connection string" providername="system.data.sqlclient" />       </connectionstrings>     </configuration>  

odata (or in case wcf data services client) doesn't lazy load navigation properties (that's properties created relationships called). have ask explicitely.

you can either front, through expand, can ask original query include entities on other side of navigation property. add .expand("orders") query order entities customers.

you can later through context.loadproperty method.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -