mvc中的用户代码未处理invalidoperationexception

时间:2015-06-25 17:06:34

标签: asp.net-mvc

BHEBS.dll中发生类型&system; ofvalidoperation异常的例外,但用户代码未处理。 附加信息:BHEBS.fService:LastChgUserId字段是必需的。

public int PostService(ModalfService objSR)         {             int i = -1;

            using (EBStestConnection db = new EBStestConnection())
            {
                fService entitysr = new fService();
                entitysr = objSR.ToEntity();
                db.fServices.Add(entitysr);



                //db.SaveChanges();
                //i = entitysr.Id;

                try
                {
                    db.SaveChanges();
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;

                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {

                        foreach (var validationError in validationErrors.ValidationErrors)
                        {

                            string message = string.Format("{0}:{1}",
                                validationErrors.Entry.Entity.ToString(),
                                validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }


        return i;
    }

0 个答案:

没有答案
相关问题