部分 class 不工作
Partial class not working
我在内联网上工作,有一个由 EntityFramework 为我的员工数据库生成的长模型,我想从中分离密码,因为它是部分 class我想我会创建这个:
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 怎么会像那样失败?
从上面的示例中,我看不到字段 _Password
的设置位置。如果方法 OnPasswordChanging、ReportPropertyChanging、ReportPropertyChanged 或 OnPasswordChanged 中的 none 执行此操作 - 然后设置 _Password = value
.
即
set
{
OnPasswordChanging(value);
ReportPropertyChanging("Password");
_Password = value;
ReportPropertyChanged("Password");
OnPasswordChanged();
}
我在内联网上工作,有一个由 EntityFramework 为我的员工数据库生成的长模型,我想从中分离密码,因为它是部分 class我想我会创建这个:
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 怎么会像那样失败?
从上面的示例中,我看不到字段 _Password
的设置位置。如果方法 OnPasswordChanging、ReportPropertyChanging、ReportPropertyChanged 或 OnPasswordChanged 中的 none 执行此操作 - 然后设置 _Password = value
.
即
set
{
OnPasswordChanging(value);
ReportPropertyChanging("Password");
_Password = value;
ReportPropertyChanged("Password");
OnPasswordChanged();
}