
namespace Hierarchy_RIA.Web
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.ComponentModel.DataAnnotations;
	using System.Data;
	using System.Linq;
	using System.ServiceModel.DomainServices.EntityFramework;
	using System.ServiceModel.DomainServices.Hosting;
	using System.ServiceModel.DomainServices.Server;
	using System.Data.Objects;


	// Implements application logic using the NorthwindEntities context.
	// TODO: Add your application logic to these methods or in additional methods.
	// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
	// Also consider adding roles to restrict access as appropriate.
	// [RequiresAuthentication]
	[EnableClientAccess()]
	public class NWDomainService : LinqToEntitiesDomainService<NorthwindEntities>
	{

		// TODO:
		// Consider constraining the results of your query method.  If you need additional input you can
		// add parameters to this method or create additional query methods with different names.
		// To support paging you will need to add ordering to the 'Orders' query.
		public IQueryable<Order> GetOrders()
		{
			return ((ObjectQuery<Order>) this.ObjectContext.Orders.OrderBy(o => o.OrderID).Where(o => o.Order_Details.Count > 0).Take(10)).Include("Order_Details");
		}

		public void InsertOrder(Order order)
		{
			if ((order.EntityState != EntityState.Detached))
			{
				this.ObjectContext.ObjectStateManager.ChangeObjectState(order, EntityState.Added);
			}
			else
			{
				this.ObjectContext.Orders.AddObject(order);
			}
		}

		public void UpdateOrder(Order currentOrder)
		{
			this.ObjectContext.Orders.AttachAsModified(currentOrder, this.ChangeSet.GetOriginal(currentOrder));
		}

		public void DeleteOrder(Order order)
		{
			if ((order.EntityState == EntityState.Detached))
			{
				this.ObjectContext.Orders.Attach(order);
			}
			this.ObjectContext.Orders.DeleteObject(order);
		}

		// TODO:
		// Consider constraining the results of your query method.  If you need additional input you can
		// add parameters to this method or create additional query methods with different names.
		// To support paging you will need to add ordering to the 'Order_Details' query.
		public IQueryable<Order_Detail> GetOrder_Details()
		{
			return this.ObjectContext.Order_Details;
		}

		public void InsertOrder_Detail(Order_Detail order_Detail)
		{
			if ((order_Detail.EntityState != EntityState.Detached))
			{
				this.ObjectContext.ObjectStateManager.ChangeObjectState(order_Detail, EntityState.Added);
			}
			else
			{
				this.ObjectContext.Order_Details.AddObject(order_Detail);
			}
		}

		public void UpdateOrder_Detail(Order_Detail currentOrder_Detail)
		{
			this.ObjectContext.Order_Details.AttachAsModified(currentOrder_Detail, this.ChangeSet.GetOriginal(currentOrder_Detail));
		}

		public void DeleteOrder_Detail(Order_Detail order_Detail)
		{
			if ((order_Detail.EntityState == EntityState.Detached))
			{
				this.ObjectContext.Order_Details.Attach(order_Detail);
			}
			this.ObjectContext.Order_Details.DeleteObject(order_Detail);
		}
	}
}


