Click here to Skip to main content
15,886,137 members
Articles / Database Development / SQL Server

Reattaching Entity Graphs with the Entity Framework

Rate me:
Please Sign up or sign in to vote.
4.85/5 (39 votes)
10 Mar 2009CPOL15 min read 288.7K   1.7K   97  
A generic method for attaching detached object graphs to an Entity Framework context
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeProject.Data.Entity;

namespace InvoiceSample
{
	partial class SampleInvoiceContext
	{
		partial void OnContextCreated()
		{
			new OptimisticConcurrencyManager(this);
		}
	}

	[AssociationEndBehaviorAttribute("Lines", Owned=true)]
	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class Invoice {

		/// <summary>
		/// TotalPrice, an additional derived property.
		/// </summary>
		public Decimal TotalPrice
		{
			get {
				Decimal totalPrice = 0m;
				foreach (InvoiceLine line in this.Lines)
				{
					if (line.LinePrice.HasValue)
						totalPrice += line.LinePrice.Value;
				}
				return totalPrice;
			}
		}

		public override string ToString()
		{
			return String.Format("Invoice {0} ({1})", this.Identifier, this.InvoiceDate);
		}
	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class InvoiceLine {

		/// <summary>
		/// LinePrice, an additional derived property.
		/// </summary>
		public Decimal? LinePrice
		{
			get { return (this.Quantity.HasValue && this.UnitPrice.HasValue) ? (decimal?)((decimal)this.Quantity.Value * this.UnitPrice.Value) : null; }
		}

		public override string ToString()
		{
			return String.Format("InvoiceLine {0} ({1}:{2}, {3}x${4})", this.ID, this.Product.ID, this.Product.Label, this.Quantity, this.UnitPrice);
		}
	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class Customer {

		public override string ToString()
		{
			return String.Format("Customer {0} ({1})", this.Identifier, this.Name);
		}

	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class Product {

		public override string ToString()
		{
			return String.Format("Product {0} ({1})", this.ID, this.Label);
		}

	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class Supplier {

		public override string ToString()
		{
			return String.Format("Supplier {0} ({1})", this.ID, this.Name);
		}

	}

	[OptimisticConcurrency("LastChanged", typeof(LocalDateTimeConcurrencyResolver))]
	partial class CatalogFolder
	{

		public override string ToString()
		{
			return String.Format("CatalogFolder {0} ({1})", this.ID, this.Name);
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AREBIS
Belgium Belgium
Senior Software Architect and independent consultant.

Comments and Discussions