Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Beginner's Tutorial on Performing Validations in Entity Framework using Partial Methods

, 6 Oct 2012 CPOL
This article talks about performing validations while using Entity framework. We will see how the Entities facilitate validations using partial methods.
EntityFrameworkSample-noexe.zip
EntityFrameworkSample
DataAccessLayer
MotorBikeShopEntities.edmx
obj
Debug
edmxResourcesToEmbed
MotorBikeShopEntities.csdl
MotorBikeShopEntities.msl
MotorBikeShopEntities.ssdl
Properties
EntityFrameworkSample.suo
EntityFrameworkTestCosnsole
bin
Debug
EntityFrameworkTestCosnsole.vshost.exe.manifest
obj
x86
Debug
Properties
PartialClassAndMethodsDemo.zip
PartialClassAndMethodsDemo
PartialClassAndMethodsDemo
Properties
PartialClassAndMethodsDemo.suo
//------------------------------------------------------------------------------
// <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()]
namespace DataAccessLayer
{
    #region Contexts
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class MotorBikeShopDbEntities : ObjectContext
    {
        #region Constructors
    
        /// <summary>
        /// Initializes a new MotorBikeShopDbEntities object using the connection string found in the 'MotorBikeShopDbEntities' section of the application configuration file.
        /// </summary>
        public MotorBikeShopDbEntities() : base("name=MotorBikeShopDbEntities", "MotorBikeShopDbEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new MotorBikeShopDbEntities object.
        /// </summary>
        public MotorBikeShopDbEntities(string connectionString) : base(connectionString, "MotorBikeShopDbEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new MotorBikeShopDbEntities object.
        /// </summary>
        public MotorBikeShopDbEntities(EntityConnection connection) : base(connection, "MotorBikeShopDbEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        #endregion
    
        #region Partial Methods
    
        partial void OnContextCreated();
    
        #endregion
    
        #region ObjectSet Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Bike> Bikes
        {
            get
            {
                if ((_Bikes == null))
                {
                    _Bikes = base.CreateObjectSet<Bike>("Bikes");
                }
                return _Bikes;
            }
        }
        private ObjectSet<Bike> _Bikes;

        #endregion

        #region AddTo Methods
    
        /// <summary>
        /// Deprecated Method for adding a new object to the Bikes EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToBikes(Bike bike)
        {
            base.AddObject("Bikes", bike);
        }

        #endregion

    }

    #endregion

    #region Entities
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="MotorBikeShopDbModel", Name="Bike")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Bike : EntityObject
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new Bike object.
        /// </summary>
        /// <param name="bikeID">Initial value of the BikeID property.</param>
        public static Bike CreateBike(global::System.Int32 bikeID)
        {
            Bike bike = new Bike();
            bike.BikeID = bikeID;
            return bike;
        }

        #endregion

        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 BikeID
        {
            get
            {
                return _BikeID;
            }
            set
            {
                if (_BikeID != value)
                {
                    OnBikeIDChanging(value);
                    ReportPropertyChanging("BikeID");
                    _BikeID = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("BikeID");
                    OnBikeIDChanged();
                }
            }
        }
        private global::System.Int32 _BikeID;
        partial void OnBikeIDChanging(global::System.Int32 value);
        partial void OnBikeIDChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String BikeName
        {
            get
            {
                return _BikeName;
            }
            set
            {
                OnBikeNameChanging(value);
                ReportPropertyChanging("BikeName");
                _BikeName = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("BikeName");
                OnBikeNameChanged();
            }
        }
        private global::System.String _BikeName;
        partial void OnBikeNameChanging(global::System.String value);
        partial void OnBikeNameChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Modelumber
        {
            get
            {
                return _Modelumber;
            }
            set
            {
                OnModelumberChanging(value);
                ReportPropertyChanging("Modelumber");
                _Modelumber = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Modelumber");
                OnModelumberChanged();
            }
        }
        private global::System.String _Modelumber;
        partial void OnModelumberChanging(global::System.String value);
        partial void OnModelumberChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Manufacturer
        {
            get
            {
                return _Manufacturer;
            }
            set
            {
                OnManufacturerChanging(value);
                ReportPropertyChanging("Manufacturer");
                _Manufacturer = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Manufacturer");
                OnManufacturerChanged();
            }
        }
        private global::System.String _Manufacturer;
        partial void OnManufacturerChanging(global::System.String value);
        partial void OnManufacturerChanged();

        #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)

Share

About the Author

Rahul Rajat Singh
Architect
India India
I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.
  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4
 
If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 6 Oct 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid