//------------------------------------------------------------------------------
// This is auto-generated code.
//------------------------------------------------------------------------------
// This code was generated by Entity Developer tool using LinqConnect template.
// Code is generated on: 14.10.2015 18:37:41
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//------------------------------------------------------------------------------

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;

namespace S11Context
{

    /// <summary>
    /// Контрагенты договора
    /// </summary>
    [Table(Name = @"s11.Contract_Contragent")]
    public partial class ContractContragent : INotifyPropertyChanging, INotifyPropertyChanged    
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(System.String.Empty);

        private int _Id;

        private bool _Deleted;

        private int _MasterId;

        private int _InsurerId;

        private int _PayerId;

        private int _PayerAccountId;

        private string _PayerKBK;

        private System.DateTime _BegDate;

        private System.DateTime _EndDate;

        private EntityRef<Contract> _Contract;
    
        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnIdChanging(int value);
        partial void OnIdChanged();
        partial void OnDeletedChanging(bool value);
        partial void OnDeletedChanged();
        partial void OnMasterIdChanging(int value);
        partial void OnMasterIdChanged();
        partial void OnInsurerIdChanging(int value);
        partial void OnInsurerIdChanged();
        partial void OnPayerIdChanging(int value);
        partial void OnPayerIdChanged();
        partial void OnPayerAccountIdChanging(int value);
        partial void OnPayerAccountIdChanged();
        partial void OnPayerKBKChanging(string value);
        partial void OnPayerKBKChanged();
        partial void OnBegDateChanging(System.DateTime value);
        partial void OnBegDateChanged();
        partial void OnEndDateChanging(System.DateTime value);
        partial void OnEndDateChanged();
        #endregion

        public ContractContragent()
        {
            this._Contract  = default(EntityRef<Contract>);
            OnCreated();
        }

    
        /// <summary>
        /// There are no comments for Id in the schema.
        /// </summary>
        [Column(Name = @"id", Storage = "_Id", AutoSync = AutoSync.OnInsert, CanBeNull = false, DbType = "INT(10) NOT NULL AUTO_INCREMENT", IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id
        {
            get
            {
                return this._Id;
            }
            set
            {
                if (this._Id != value)
                {
                    this.OnIdChanging(value);
                    this.SendPropertyChanging();
                    this._Id = value;
                    this.SendPropertyChanged("Id");
                    this.OnIdChanged();
                }
            }
        }

    
        /// <summary>
        /// Отметка удаления записи
        /// </summary>
        [Column(Name = @"deleted", Storage = "_Deleted", CanBeNull = false, DbType = "TINYINT(1) NOT NULL")]
        public bool Deleted
        {
            get
            {
                return this._Deleted;
            }
            set
            {
                if (this._Deleted != value)
                {
                    this.OnDeletedChanging(value);
                    this.SendPropertyChanging();
                    this._Deleted = value;
                    this.SendPropertyChanged("Deleted");
                    this.OnDeletedChanged();
                }
            }
        }

    
        /// <summary>
        /// {Contract}
        /// </summary>
        [Column(Name = @"master_id", Storage = "_MasterId", CanBeNull = false, DbType = "INT(10) NOT NULL")]
        public int MasterId
        {
            get
            {
                return this._MasterId;
            }
            set
            {
                if (this._MasterId != value)
                {
                    if (this._Contract.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }

                    this.OnMasterIdChanging(value);
                    this.SendPropertyChanging();
                    this._MasterId = value;
                    this.SendPropertyChanged("MasterId");
                    this.OnMasterIdChanged();
                }
            }
        }

    
        /// <summary>
        /// Страхователь из полиса {Organisation}
        /// </summary>
        [Column(Name = @"insurer_id", Storage = "_InsurerId", CanBeNull = false, DbType = "INT(10) NOT NULL")]
        public int InsurerId
        {
            get
            {
                return this._InsurerId;
            }
            set
            {
                if (this._InsurerId != value)
                {
                    this.OnInsurerIdChanging(value);
                    this.SendPropertyChanging();
                    this._InsurerId = value;
                    this.SendPropertyChanged("InsurerId");
                    this.OnInsurerIdChanged();
                }
            }
        }

    
        /// <summary>
        /// Плательщик {Organisation}
        /// </summary>
        [Column(Name = @"payer_id", Storage = "_PayerId", CanBeNull = false, DbType = "INT(10) NOT NULL")]
        public int PayerId
        {
            get
            {
                return this._PayerId;
            }
            set
            {
                if (this._PayerId != value)
                {
                    this.OnPayerIdChanging(value);
                    this.SendPropertyChanging();
                    this._PayerId = value;
                    this.SendPropertyChanged("PayerId");
                    this.OnPayerIdChanged();
                }
            }
        }

    
        /// <summary>
        /// Расчетный счет плательщика {OrgAccount}
        /// </summary>
        [Column(Name = @"payerAccount_id", Storage = "_PayerAccountId", CanBeNull = false, DbType = "INT(10) NOT NULL")]
        public int PayerAccountId
        {
            get
            {
                return this._PayerAccountId;
            }
            set
            {
                if (this._PayerAccountId != value)
                {
                    this.OnPayerAccountIdChanging(value);
                    this.SendPropertyChanging();
                    this._PayerAccountId = value;
                    this.SendPropertyChanged("PayerAccountId");
                    this.OnPayerAccountIdChanged();
                }
            }
        }

    
        /// <summary>
        /// КБК плательщика
        /// </summary>
        [Column(Name = @"payerKBK", Storage = "_PayerKBK", CanBeNull = false, DbType = "VARCHAR(30) NOT NULL")]
        public string PayerKBK
        {
            get
            {
                return this._PayerKBK;
            }
            set
            {
                if (this._PayerKBK != value)
                {
                    this.OnPayerKBKChanging(value);
                    this.SendPropertyChanging();
                    this._PayerKBK = value;
                    this.SendPropertyChanged("PayerKBK");
                    this.OnPayerKBKChanged();
                }
            }
        }

    
        /// <summary>
        /// Дата начала действия строки контрагента
        /// </summary>
        [Column(Name = @"begDate", Storage = "_BegDate", CanBeNull = false, DbType = "DATE NOT NULL")]
        public System.DateTime BegDate
        {
            get
            {
                return this._BegDate;
            }
            set
            {
                if (this._BegDate != value)
                {
                    this.OnBegDateChanging(value);
                    this.SendPropertyChanging();
                    this._BegDate = value;
                    this.SendPropertyChanged("BegDate");
                    this.OnBegDateChanged();
                }
            }
        }

    
        /// <summary>
        /// Дата окончания действия строки контрагента
        /// </summary>
        [Column(Name = @"endDate", Storage = "_EndDate", CanBeNull = false, DbType = "DATE NOT NULL")]
        public System.DateTime EndDate
        {
            get
            {
                return this._EndDate;
            }
            set
            {
                if (this._EndDate != value)
                {
                    this.OnEndDateChanging(value);
                    this.SendPropertyChanging();
                    this._EndDate = value;
                    this.SendPropertyChanged("EndDate");
                    this.OnEndDateChanged();
                }
            }
        }

    
        /// <summary>
        /// There are no comments for Contract in the schema.
        /// </summary>
        [Devart.Data.Linq.Mapping.Association(Name="Contract_ContractContragent", Storage="_Contract", ThisKey="MasterId", IsForeignKey=true)]
        public Contract Contract
        {
            get
            {
                return this._Contract.Entity;
            }
            set
            {
                Contract previousValue = this._Contract.Entity;
                if ((previousValue != value) || (this._Contract.HasLoadedOrAssignedValue == false))
                {
                    this.SendPropertyChanging();
                    if (previousValue != null)
                    {
                        this._Contract.Entity = null;
                        previousValue.ContractContragents.Remove(this);
                    }
                    this._Contract.Entity = value;
                    if (value != null)
                    {
                        this._MasterId = value.Id;
                        value.ContractContragents.Add(this);
                    }
                    else
                    {
                        this._MasterId = default(int);
                    }
                    this.SendPropertyChanged("Contract");
                }
            }
        }
   
        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if (this.PropertyChanging != null)
                this.PropertyChanging(this, emptyChangingEventArgs);
        }

        protected virtual void SendPropertyChanging(System.String propertyName) 
        {
            if (this.PropertyChanging != null)
                this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }

        protected virtual void SendPropertyChanged(System.String propertyName)
        {
             if (this.PropertyChanged != null)
                 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
