// Create a new State
        public State Create(State mState)
        {
            var result = 0;

            EDM.State edmState = StateMapper.MapOneDMtoEDM(mState);

            try {
                this._dbContext.States.Add(edmState);
                result = this._dbContext.SaveChanges();

                // DbContext sets Id to the new Identity value.
                mState.Id = edmState.Id;
            }
            catch (System.Data.UpdateException ex) {
                if (ex.InnerException != null && ex.InnerException is System.Data.SqlClient.SqlException &&
                    ((System.Data.SqlClient.SqlException)ex.InnerException).ErrorCode == 8152)
                {
                    throw ex.InnerException;
                }
                else
                {
                    throw ex;
                }
            }

            return(mState);
        }
        // GetById
        public State GetById(int id)
        {
            EDM.State edmState = default(EDM.State);

            edmState = _dbContext.States
                       .Where(edm => edm.Id == id).FirstOrDefault();

            return(StateMapper.MapOneEDMtoDM(edmState));
        }
        public static EDM.State MapOneDMtoEDM(State mState) {
            EDM.State edmState = new EDM.State();

			edmState.Id = mState.Id;
			edmState.Name = mState.Name;
			edmState.Code = mState.Code;
			edmState.ZipCodeRange = mState.ZipCodeRange;
			edmState.MarkedToDelete = bool.Parse(mState.MarkedToDelete);

            return edmState;
        }
        public static EDM.State MapOneDMtoEDM(State mState)
        {
            EDM.State edmState = new EDM.State();

            edmState.Id             = mState.Id;
            edmState.Name           = mState.Name;
            edmState.Code           = mState.Code;
            edmState.ZipCodeRange   = mState.ZipCodeRange;
            edmState.MarkedToDelete = bool.Parse(mState.MarkedToDelete);

            return(edmState);
        }
        public static State MapOneEDMtoDM(EDM.State edmState)
        {
            State mState = new State();

            mState.Id             = edmState.Id;
            mState.Name           = edmState.Name;
            mState.Code           = edmState.Code;
            mState.ZipCodeRange   = edmState.ZipCodeRange;
            mState.MarkedToDelete = edmState.MarkedToDelete.ToString();

            return(mState);
        }
        // Update an existing State
        public int Update(State mState)
        {
            var result = 0;

            EDM.State edmLoadedState = null;
            EDM.State edmState       = StateMapper.MapOneDMtoEDM(mState);

            try {
                // Load object into context (entity framework)
                edmLoadedState = _dbContext.States.Where(edm => edm.Id == mState.Id).FirstOrDefault();

                if (edmLoadedState == null)   //not found?
                {
                    throw new Exception("State not found to update");
                }
                else
                {
                    // Update
                    _dbContext.Entry(edmLoadedState).CurrentValues.SetValues(edmState);
                }

                // Save in data access (entity framework)
                result = this._dbContext.SaveChanges();
            }
            catch (System.Data.UpdateException ex) {
                if (ex.InnerException != null && ex.InnerException is System.Data.SqlClient.SqlException &&
                    ((System.Data.SqlClient.SqlException)ex.InnerException).ErrorCode == 8152)
                {
                    throw ex.InnerException;
                }
                else
                {
                    throw ex;
                }
            }

            return(result);
        }
        // Delete an existing State
        public int Delete(int id)
        {
            var result = 0;

            EDM.State edmLoadedState = null;

            try {
                // Load object into context (entity framework)
                edmLoadedState = _dbContext.States.Where(edm => edm.Id == id).FirstOrDefault();

                // Modify the context
                if (edmLoadedState == null)   //not found?
                {
                    throw new Exception("State not found to delete");
                }
                else
                {
                    // Delete
                    this._dbContext.States.Remove(edmLoadedState);
                }

                // Save in data access (entity framework)
                result = this._dbContext.SaveChanges();
            }
            catch (System.Data.UpdateException ex) {
                if (ex.InnerException != null && ex.InnerException is System.Data.SqlClient.SqlException &&
                    ((System.Data.SqlClient.SqlException)ex.InnerException).ErrorCode == 8152)
                {
                    throw ex.InnerException;
                }
                else
                {
                    throw ex;
                }
            }

            return(result);
        }