Beispiel #1
0
        // Create a new Entity
        public Entity Create(Entity mEntity)
        {
            var result = 0;

            EDM.Entity edmEntity = EntityMapper.MapOneDMtoEDM(mEntity);

            try {
                this._dbContext.Entities.Add(edmEntity);
                result = this._dbContext.SaveChanges();

                // DbContext sets Id to the new Identity value.
                mEntity.Id = edmEntity.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(mEntity);
        }
        public static EDM.Entity MapOneDMtoEDM(Entity mEntity) {
            EDM.Entity edmEntity = new EDM.Entity();

			edmEntity.Id = mEntity.Id;
			edmEntity.FirstName = mEntity.FirstName;
			edmEntity.LastName = mEntity.LastName;

            return edmEntity;
        }
Beispiel #3
0
        // GetById
        public Entity GetById(int id)
        {
            EDM.Entity edmEntity = default(EDM.Entity);

            edmEntity = _dbContext.Entities
                        .Where(edm => edm.Id == id).FirstOrDefault();

            return(EntityMapper.MapOneEDMtoDM(edmEntity));
        }
Beispiel #4
0
        public static EDM.Entity MapOneDMtoEDM(Entity mEntity)
        {
            EDM.Entity edmEntity = new EDM.Entity();

            edmEntity.Id        = mEntity.Id;
            edmEntity.FirstName = mEntity.FirstName;
            edmEntity.LastName  = mEntity.LastName;

            return(edmEntity);
        }
Beispiel #5
0
        public static Entity MapOneEDMtoDM(EDM.Entity edmEntity)
        {
            Entity mEntity = new Entity();

            mEntity.Id        = edmEntity.Id;
            mEntity.FirstName = edmEntity.FirstName;
            mEntity.LastName  = edmEntity.LastName;

            return(mEntity);
        }
Beispiel #6
0
        // Update an existing Entity
        public int Update(Entity mEntity)
        {
            var result = 0;

            EDM.Entity edmLoadedEntity = null;
            EDM.Entity edmEntity       = EntityMapper.MapOneDMtoEDM(mEntity);

            try {
                // Load object into context (entity framework)
                edmLoadedEntity = _dbContext.Entities.Where(edm => edm.Id == mEntity.Id).FirstOrDefault();

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

                // 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);
        }
Beispiel #7
0
        // Delete an existing Entity
        public int Delete(int id)
        {
            var result = 0;

            EDM.Entity edmLoadedEntity = null;

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

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

                // 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);
        }