我已经实现了以下代码:
通用存储库:
public abstract class BaseRepository<T> : IRepository<T> where T : class
{
public void Update(T entity)
{
using (Context context = new Context())
{
context.Entry(entity).State = EntityState.Modified;
context.SaveChanges();
}
}
}
特定存储库:
public class RoleRepository : BaseRepository<Role>
{
}
业务层:
public class RoleBL
{
static RoleRepository rolRepository = new RoleRepository();
public static Role Edit(int RoleId)
{
return rolRepository.Single(r => r.RoleId == RoleId);
}
public static void Update(Role roleData)
{
rolRepository.Update(roleData);
}
}
UI图层:
private void btnRefresh_Click(object sender, EventArgs e)
{
Role role = new Role()
{
RoleId = this.roleId,
Name = txtName.Text,
Description = txtDescription.Text
};
RoleBL.Update(role);
}
当我单击“刷新”按钮时,出现以下错误:
EntityFramework.dll中出现'System.Data.Entity.Validation.DbEntityValidationException'类型的第一次机会异常
其他信息:一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。