Business entity transferred between layersHi<br/><br/>I am now trying to migrate my knowledge from LINQ to SQL to the Entity Framework.<br/>One of the main questions I have is the following.<br/><br/>In my sample code I am using the Northwind database.<br/>I would like to transfer to the UI a business object which is a customer with the ID 'ALFKI'.<br/>I would also like to transfer in the same business object all those related 'orders' which happened after 1998-01-01.<br/>One more important thing is that the UI should only see the ID and contact name for the customer,<br/>and he/she should only see the shipcity for the orders of the actual customer.<br/><br/>Below is my try.<br/>The problem is with it that now the UI can see all fields for the customer, and all fields for the related orders.<br/>Another problem is that I am not sure whether 'Attach' is the proper method to use and I am not sure whether<br/>the EF-designer-generated Customer object is which I have to transfer between the app layers.<br/><br/> <pre lang="x-c#">Customer customer = null; using (NorthwindEntities dc = new NorthwindEntities()) { customer = (from customers in dc.CustomerSet where customers.CustomerID==&quot;ALFKI&quot; select customers).FirstOrDefault(); customer.Orders.Attach( from orders in dc.OrderSet where orders.OrderDate.HasValue &amp;&amp; orders.OrderDate.Value.CompareTo(new DateTime(1998, 1, 1)) &gt; 0 &amp;&amp; orders.Customers.CustomerID == &quot;ALFKI&quot; select orders); } foreach (Order order in from orders in customer.Orders orderby orders.OrderDate select orders) { Console.WriteLine(order.OrderDate.ToString() + &quot; &quot; + order.ShipCity + &quot; &quot; + order.Customers.CustomerID); } Console.ReadLine();</pre> Thanks for any links or code fragments.© 2009 Microsoft Corporation. All rights reserved.Mon, 06 Jul 2009 12:58:50 Z237ec2e3-98dd-407b-9a87-e46440c400cbhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/237ec2e3-98dd-407b-9a87-e46440c400cb#237ec2e3-98dd-407b-9a87-e46440c400cbhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/237ec2e3-98dd-407b-9a87-e46440c400cb#237ec2e3-98dd-407b-9a87-e46440c400cbvmwarehttp://social.msdn.microsoft.com/Profile/en-US/?user=vmwareBusiness entity transferred between layersHi<br/><br/>I am now trying to migrate my knowledge from LINQ to SQL to the Entity Framework.<br/>One of the main questions I have is the following.<br/><br/>In my sample code I am using the Northwind database.<br/>I would like to transfer to the UI a business object which is a customer with the ID 'ALFKI'.<br/>I would also like to transfer in the same business object all those related 'orders' which happened after 1998-01-01.<br/>One more important thing is that the UI should only see the ID and contact name for the customer,<br/>and he/she should only see the shipcity for the orders of the actual customer.<br/><br/>Below is my try.<br/>The problem is with it that now the UI can see all fields for the customer, and all fields for the related orders.<br/>Another problem is that I am not sure whether 'Attach' is the proper method to use and I am not sure whether<br/>the EF-designer-generated Customer object is which I have to transfer between the app layers.<br/><br/> <pre lang="x-c#">Customer customer = null; using (NorthwindEntities dc = new NorthwindEntities()) { customer = (from customers in dc.CustomerSet where customers.CustomerID==&quot;ALFKI&quot; select customers).FirstOrDefault(); customer.Orders.Attach( from orders in dc.OrderSet where orders.OrderDate.HasValue &amp;&amp; orders.OrderDate.Value.CompareTo(new DateTime(1998, 1, 1)) &gt; 0 &amp;&amp; orders.Customers.CustomerID == &quot;ALFKI&quot; select orders); } foreach (Order order in from orders in customer.Orders orderby orders.OrderDate select orders) { Console.WriteLine(order.OrderDate.ToString() + &quot; &quot; + order.ShipCity + &quot; &quot; + order.Customers.CustomerID); } Console.ReadLine();</pre> Thanks for any links or code fragments.Thu, 02 Jul 2009 14:08:22 Z2009-07-02T14:08:22Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/237ec2e3-98dd-407b-9a87-e46440c400cb#e22827f2-1bc4-41e1-ad12-af1568b1e5cehttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/237ec2e3-98dd-407b-9a87-e46440c400cb#e22827f2-1bc4-41e1-ad12-af1568b1e5ceIdo Flatow.http://social.msdn.microsoft.com/Profile/en-US/?user=Ido%20Flatow.Business entity transferred between layersHi,<br/><br/>As for getting the Orders, you're code can be changed to:<br/><br/>orders = customer.Orders.CreateObjectQuery().Where(o=&gt;o.OrderDate != null &amp;&amp; o.OrderDate.Value &gt; new DateTime(1998,1,1)).ToList();<br/><br/>This will extract the orders of the customer which apply to you condition, but will not add them to the Orders collection of the specific customer.<br/><br/>As for your requirement to see only some of the fields of the entity, this will make you change your query and get an anonymous type:<br/>var shortDataOfCustomer = (<br/>from c in dc.CustomerSet <br/>where c.CustomerID == &quot;ALFKI&quot;<br/>select new {c.CustomerID, c.CustomerName}).FirstOrDefault();<br/><br/>If you want to return Customer objects and not anonymous types, you can change the new {} to new Customer() {CustomerID = c.CustomerID, CustomerName=c.CustomerName}<br/>of course in this case the entities won't be a part of the ObjectStateManager (meaning the won't be tracked for changes).<br/>           Thu, 02 Jul 2009 19:19:30 Z2009-07-02T19:19:30Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/237ec2e3-98dd-407b-9a87-e46440c400cb#59a71083-9b56-4817-afa5-47e9921a1720http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/237ec2e3-98dd-407b-9a87-e46440c400cb#59a71083-9b56-4817-afa5-47e9921a1720vmwarehttp://social.msdn.microsoft.com/Profile/en-US/?user=vmwareBusiness entity transferred between layersThanks for your answer.<br/><br/>You are right that I could have written the dates comparison simpler - now I corrected this.<br/><br/>For hiding specifig table fields data from the user I found that I would set the actual entity fields to internal (from public).<br/> <pre lang="x-c#">Customer customer = null; using (NorthwindEntities dc = new NorthwindEntities()) { customer = (from customers in dc.CustomerSet where customers.CustomerID==&quot;ALFKI&quot; select customers).FirstOrDefault(); customer.Orders.Attach( from orders in dc.OrderSet where orders.OrderDate &gt; new DateTime(1998, 1, 1)&amp;&amp; orders.Customer.CustomerID == &quot;ALFKI&quot; select orders); } foreach (Order order in from orders in customer.Orders orderby orders.OrderDate select orders) { Console.WriteLine(order.OrderDate.ToString() + &quot; &quot; + order.ShipCity + &quot; &quot; + order.Customer.CustomerID); } Console.ReadLine();<br/></pre> <br/>I still have these two remaining questions:<br/>1) <br/>Is Attach the proper method for transferring entities with some specific relations?<br/>2) <br/>How can I update data after the UI changes it?<br/>Updating data only works if the ObjectContext is not disposed.<br/>But when my business logic component recieves a modified Customer entity from the UI, I couldn't find the proper method to save changes.<br/>I tried these two (I added the related error messages):<br/> <pre lang="x-c#">using (NorthwindEntities dc = new NorthwindEntities()) { dc.AddToCustomerSet(customer); dc.SaveChanges(); }<br/></pre> <span style="font-family:Courier New"><em>The object cannot be added to the ObjectStateManager because it already has an EntityKey. Use ObjectContext.Attach to attach an object that has an existing key.<br/></em> <pre lang="x-c#">using (NorthwindEntities dc = new NorthwindEntities()) { dc.Attach(customer); dc.SaveChanges(); }</pre> <em>An entity object cannot be referenced by multiple instances of IEntityChangeTracker.<br/><br/></em>Thanks.</span>Mon, 06 Jul 2009 12:58:49 Z2009-07-06T12:58:49Z