Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Implementing Repository Pattern With Entity Framework

Rate me:
Please Sign up or sign in to vote.
4.77/5 (56 votes)
2 Nov 2012CPOL6 min read 448.4K   5.7K   215  
Implement Repository Pattern in data access layer with Entity Framework 4.0 and below version (working with ObjectContext and EntityObject )
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;

[assembly: EdmSchemaAttribute()]
#region EDM Relationship Metadata

[assembly: EdmRelationshipAttribute("ProjectCodeModel", "FK_dbo_BlogPosts_dbo_Categories_CategoryId", "Categories", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(RepositoryProjectSample.Category), "BlogPosts", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(RepositoryProjectSample.BlogPost), true)]

#endregion

namespace RepositoryProjectSample
{
    #region Contexts
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class ProjectCodeEntities : ObjectContext
    {
        #region Constructors
    
        /// <summary>
        /// Initializes a new ProjectCodeEntities object using the connection string found in the 'ProjectCodeEntities' section of the application configuration file.
        /// </summary>
        public ProjectCodeEntities() : base("name=ProjectCodeEntities", "ProjectCodeEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new ProjectCodeEntities object.
        /// </summary>
        public ProjectCodeEntities(string connectionString) : base(connectionString, "ProjectCodeEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new ProjectCodeEntities object.
        /// </summary>
        public ProjectCodeEntities(EntityConnection connection) : base(connection, "ProjectCodeEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        #endregion
    
        #region Partial Methods
    
        partial void OnContextCreated();
    
        #endregion
    
        #region ObjectSet Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<C__MigrationHistory> C__MigrationHistory
        {
            get
            {
                if ((_C__MigrationHistory == null))
                {
                    _C__MigrationHistory = base.CreateObjectSet<C__MigrationHistory>("C__MigrationHistory");
                }
                return _C__MigrationHistory;
            }
        }
        private ObjectSet<C__MigrationHistory> _C__MigrationHistory;
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<BlogPost> BlogPosts
        {
            get
            {
                if ((_BlogPosts == null))
                {
                    _BlogPosts = base.CreateObjectSet<BlogPost>("BlogPosts");
                }
                return _BlogPosts;
            }
        }
        private ObjectSet<BlogPost> _BlogPosts;
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Category> Categories
        {
            get
            {
                if ((_Categories == null))
                {
                    _Categories = base.CreateObjectSet<Category>("Categories");
                }
                return _Categories;
            }
        }
        private ObjectSet<Category> _Categories;

        #endregion

        #region AddTo Methods
    
        /// <summary>
        /// Deprecated Method for adding a new object to the C__MigrationHistory EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToC__MigrationHistory(C__MigrationHistory c__MigrationHistory)
        {
            base.AddObject("C__MigrationHistory", c__MigrationHistory);
        }
    
        /// <summary>
        /// Deprecated Method for adding a new object to the BlogPosts EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToBlogPosts(BlogPost blogPost)
        {
            base.AddObject("BlogPosts", blogPost);
        }
    
        /// <summary>
        /// Deprecated Method for adding a new object to the Categories EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToCategories(Category category)
        {
            base.AddObject("Categories", category);
        }

        #endregion

    }

    #endregion

    #region Entities
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="ProjectCodeModel", Name="BlogPost")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class BlogPost : EntityObject
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new BlogPost object.
        /// </summary>
        /// <param name="id">Initial value of the Id property.</param>
        /// <param name="publishDate">Initial value of the PublishDate property.</param>
        /// <param name="categoryId">Initial value of the CategoryId property.</param>
        public static BlogPost CreateBlogPost(global::System.Guid id, global::System.DateTime publishDate, global::System.Guid categoryId)
        {
            BlogPost blogPost = new BlogPost();
            blogPost.Id = id;
            blogPost.PublishDate = publishDate;
            blogPost.CategoryId = categoryId;
            return blogPost;
        }

        #endregion

        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Guid Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    OnIdChanging(value);
                    ReportPropertyChanging("Id");
                    _Id = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Id");
                    OnIdChanged();
                }
            }
        }
        private global::System.Guid _Id;
        partial void OnIdChanging(global::System.Guid value);
        partial void OnIdChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Title
        {
            get
            {
                return _Title;
            }
            set
            {
                OnTitleChanging(value);
                ReportPropertyChanging("Title");
                _Title = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Title");
                OnTitleChanged();
            }
        }
        private global::System.String _Title;
        partial void OnTitleChanging(global::System.String value);
        partial void OnTitleChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Content
        {
            get
            {
                return _Content;
            }
            set
            {
                OnContentChanging(value);
                ReportPropertyChanging("Content");
                _Content = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Content");
                OnContentChanged();
            }
        }
        private global::System.String _Content;
        partial void OnContentChanging(global::System.String value);
        partial void OnContentChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.DateTime PublishDate
        {
            get
            {
                return _PublishDate;
            }
            set
            {
                OnPublishDateChanging(value);
                ReportPropertyChanging("PublishDate");
                _PublishDate = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("PublishDate");
                OnPublishDateChanged();
            }
        }
        private global::System.DateTime _PublishDate;
        partial void OnPublishDateChanging(global::System.DateTime value);
        partial void OnPublishDateChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Guid CategoryId
        {
            get
            {
                return _CategoryId;
            }
            set
            {
                OnCategoryIdChanging(value);
                ReportPropertyChanging("CategoryId");
                _CategoryId = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("CategoryId");
                OnCategoryIdChanged();
            }
        }
        private global::System.Guid _CategoryId;
        partial void OnCategoryIdChanging(global::System.Guid value);
        partial void OnCategoryIdChanged();

        #endregion

    
        #region Navigation Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [XmlIgnoreAttribute()]
        [SoapIgnoreAttribute()]
        [DataMemberAttribute()]
        [EdmRelationshipNavigationPropertyAttribute("ProjectCodeModel", "FK_dbo_BlogPosts_dbo_Categories_CategoryId", "Categories")]
        public Category Category
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("ProjectCodeModel.FK_dbo_BlogPosts_dbo_Categories_CategoryId", "Categories").Value;
            }
            set
            {
                ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("ProjectCodeModel.FK_dbo_BlogPosts_dbo_Categories_CategoryId", "Categories").Value = value;
            }
        }
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [BrowsableAttribute(false)]
        [DataMemberAttribute()]
        public EntityReference<Category> CategoryReference
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Category>("ProjectCodeModel.FK_dbo_BlogPosts_dbo_Categories_CategoryId", "Categories");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Category>("ProjectCodeModel.FK_dbo_BlogPosts_dbo_Categories_CategoryId", "Categories", value);
                }
            }
        }

        #endregion

    }
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="ProjectCodeModel", Name="C__MigrationHistory")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class C__MigrationHistory : EntityObject
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new C__MigrationHistory object.
        /// </summary>
        /// <param name="migrationId">Initial value of the MigrationId property.</param>
        /// <param name="model">Initial value of the Model property.</param>
        /// <param name="productVersion">Initial value of the ProductVersion property.</param>
        public static C__MigrationHistory CreateC__MigrationHistory(global::System.String migrationId, global::System.Byte[] model, global::System.String productVersion)
        {
            C__MigrationHistory c__MigrationHistory = new C__MigrationHistory();
            c__MigrationHistory.MigrationId = migrationId;
            c__MigrationHistory.Model = model;
            c__MigrationHistory.ProductVersion = productVersion;
            return c__MigrationHistory;
        }

        #endregion

        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String MigrationId
        {
            get
            {
                return _MigrationId;
            }
            set
            {
                if (_MigrationId != value)
                {
                    OnMigrationIdChanging(value);
                    ReportPropertyChanging("MigrationId");
                    _MigrationId = StructuralObject.SetValidValue(value, false);
                    ReportPropertyChanged("MigrationId");
                    OnMigrationIdChanged();
                }
            }
        }
        private global::System.String _MigrationId;
        partial void OnMigrationIdChanging(global::System.String value);
        partial void OnMigrationIdChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Byte[] Model
        {
            get
            {
                return StructuralObject.GetValidValue(_Model);
            }
            set
            {
                OnModelChanging(value);
                ReportPropertyChanging("Model");
                _Model = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("Model");
                OnModelChanged();
            }
        }
        private global::System.Byte[] _Model;
        partial void OnModelChanging(global::System.Byte[] value);
        partial void OnModelChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String ProductVersion
        {
            get
            {
                return _ProductVersion;
            }
            set
            {
                OnProductVersionChanging(value);
                ReportPropertyChanging("ProductVersion");
                _ProductVersion = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("ProductVersion");
                OnProductVersionChanged();
            }
        }
        private global::System.String _ProductVersion;
        partial void OnProductVersionChanging(global::System.String value);
        partial void OnProductVersionChanged();

        #endregion

    
    }
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="ProjectCodeModel", Name="Category")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Category : EntityObject
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new Category object.
        /// </summary>
        /// <param name="id">Initial value of the Id property.</param>
        public static Category CreateCategory(global::System.Guid id)
        {
            Category category = new Category();
            category.Id = id;
            return category;
        }

        #endregion

        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Guid Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    OnIdChanging(value);
                    ReportPropertyChanging("Id");
                    _Id = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Id");
                    OnIdChanged();
                }
            }
        }
        private global::System.Guid _Id;
        partial void OnIdChanging(global::System.Guid value);
        partial void OnIdChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                OnNameChanging(value);
                ReportPropertyChanging("Name");
                _Name = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Name");
                OnNameChanged();
            }
        }
        private global::System.String _Name;
        partial void OnNameChanging(global::System.String value);
        partial void OnNameChanged();

        #endregion

    
        #region Navigation Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [XmlIgnoreAttribute()]
        [SoapIgnoreAttribute()]
        [DataMemberAttribute()]
        [EdmRelationshipNavigationPropertyAttribute("ProjectCodeModel", "FK_dbo_BlogPosts_dbo_Categories_CategoryId", "BlogPosts")]
        public EntityCollection<BlogPost> BlogPosts
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<BlogPost>("ProjectCodeModel.FK_dbo_BlogPosts_dbo_Categories_CategoryId", "BlogPosts");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<BlogPost>("ProjectCodeModel.FK_dbo_BlogPosts_dbo_Categories_CategoryId", "BlogPosts", value);
                }
            }
        }

        #endregion

    }

    #endregion

    
}

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
Team Leader PracticePRO Software Systems Inc
United States United States
In my childhood, my uncle has shown me how to see the cloud in a close look and I understand that one can draw some elements of the Earth in the sky-canvas if he/she wants to. After that the cloud becomes closer to me and It teaches me one thing that, a deeper-look to something will give you some clues to draw your imagination. You can able to see that one which you have build-up in your mind.

Years past, I have started my career as a software engineer and has been looking for passion in my coding and development which I should be to enjoy my profession and has started asking myself- 'am I doing any engineering here?!' Is my code becoming that thing which I have designed in my mind? So to find that answer I have tried that old solution here... I have decided to come closer to my code and start analyzing them. And it is really working for me and at least it gives me the confidence that I can build something that I really want to. I can draw my thinking there through my code and can build-up my vision that I have designed in my mind. It also helps me to think out of the box, solve each problems by making blocks and make me careful on each steps.

• Morshed's Technical Blog site: http://morshedanwar.wordpress.com/

• Morshed's Technical articles those are published in Codeproject site: http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2992452

• Morshed's Linkedin profile: http://www.linkedin.com/in/morshedanwar

• Morshed's Facebook Profile : http://www.facebook.com/morshed.pulok

Beside all these I like to do - photography and music. Here is my Flickr photos : http://www.flickr.com/photos/morshed_anwar/

Comments and Discussions