部分班级不起作用

时间:2015-09-24 13:08:23

标签: c# asp.net-mvc entity-framework forms-authentication

我正在使用Intranet并且有一个由我的员工数据库的EntityFramework生成的长模型,我想从中分配密码,因为它是一个部分类我想我会创建它:

namespace CRAWebSiteMVC.Models
{
    public partial class Employee
    {
        public global::System.String Password
        {
            get
            {
                return _Password;
            }
            set
            {
                OnPasswordChanging(value);
                ReportPropertyChanging("Password");                    
                ReportPropertyChanged("Password");
                OnPasswordChanged();
            }
        }
        private global::System.String _Password;
        partial void OnPasswordChanging(global::System.String value);
        partial void OnPasswordChanged();

    }

}

但是现在,每当我尝试为密码分配一个值时,它总是会有一个空值。

例如:

  [HttpPost]
    public ActionResult Create([Bind(Exclude = "Id")] Employee objEmployee, FormCollection form)
    {
        ViewBag.CompanyList = _service.ListCompany();
        ViewBag.SupervisorList = _service.ListSupervisor();

        objEmployee.CreatedDate = System.DateTime.Now; // They will get the good value
        objEmployee.UpdatedDate = System.DateTime.Now;

        objEmployee.Password = form["Password"]; // will always be = to null, even when the form is filled
    }

这可能是什么原因?

编辑:密码现在有一个值,谢谢大家。

但现在出现以下错误:

An error occurred while updating the entries. See the inner exception for details.

此时:

 public Employee Create(Employee objEmployee)
    {
        _entities.AddToEmployees(objEmployee);
        _entities.SaveChanges(); // Fails here
        return objEmployee;
    }

SaveChange怎么会失败?

1 个答案:

答案 0 :(得分:2)

从上面的示例中我看不到字段_Password的设置位置。如果没有方法OnPasswordChanging,ReportPropertyChanging,ReportPropertyChanged或OnPasswordChanged这样做 - 那么设置_Password = value

  set
        {
            OnPasswordChanging(value);
            ReportPropertyChanging("Password");   
            _Password = value;
            ReportPropertyChanged("Password");
            OnPasswordChanged();
        }