我已经实现了通用存储库模式和UnitOfWork,但是当我尝试填充模型类的新实例时,我的控制器中出现了空引用异常。
这是我的存储库模式:
class GenericRepository<TEntity> where TEntity : class
{
internal PropmetEntities context;
internal DbSet<TEntity> dbSet;
public GenericRepository(PropmetEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get()
{
IQueryable<TEntity> query = dbSet;
return query.ToList();
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
这是我的工作单位:
class UnitOfWork
{
private PropmetEntities context = new PropmetEntities();
private GenericRepository<User> userRepository;
public GenericRepository<User> UserRepository
{
get
{
if (this.userRepository == null)
this.userRepository = new GenericRepository<User>(context);
return userRepository;
}
}
}
这是我的模型类:
public class UserGridModel
{
public int ID { get; set; }
public string Username {get; set;}
public string EMail { get; set; }
public string Surname { get; set; }
public string Role { get; set; }
public ContactInformation ContactInformation { get; set; }
public Role Role1 { get; set; }
}
这是我的控制器动作:
private UnitOfWork.UnitOfWork unitOfWork = new UnitOfWork.UnitOfWork();
[PrivilegeFilter(priv = Privileges.UserView)]
public ActionResult Grid(GridSettings set)
{
var userList = from user in unitOfWork.UserRepository.Get() select user;
var users = new List<UserGridModel>();
if (userList.Any())
{
foreach (var user in userList)
{
users.Add(new UserGridModel()
{
ID = user.ID,
Username = user.Username,
EMail = user.ContactInformation.EMail,
Surname = user.ContactInformation.Surname,
Role = user.Role1.Description
});
}
}
GridModel grid = new GridModel(set, users.Count());
grid.rows = users.Skip((set.PageIndex - 1) * set.PageSize).Take(set.PageSize).ToList();
return Json(grid, JsonRequestBehavior.AllowGet);
}
我在这里得到一个空引用异常:
users.Add(new UserGridModel()
{
ID = user.ID,
Username = user.Username,
EMail = user.ContactInformation.EMail,
Surname = user.ContactInformation.Surname,
Role = user.Role1.Description
});
答案 0 :(得分:1)
你有用户的ContactInformation吗?我的猜测是user.ContactInformation为null,并且在获取user.ContactInformation.Email时收到错误。
答案 1 :(得分:0)
可能的原因是
user.ContactInformation == null
或user.Role1 == null
。您应该在UserGridModel
创建代码中添加一些空值检查。
答案 2 :(得分:0)
您可以通过执行以下操作null
进行检查:
foreach (var user in userList.Where(x => x.ContactInformation != null && x.Role1 != null))
{
users.Add(new UserGridModel()
{
ID = user.ID,
Username = user.Username,
EMail = user.ContactInformation.EMail,
Surname = user.ContactInformation.Surname,
Role = user.Role1.Description
});
}
当其中一个属性为null
时,这至少会阻止您获取空引用异常。