";对象未设置为对象的实例“;使用在一个实例中有效但在另一个实例不起作用的代码
本文关键字:实例 对象 一个 有效 代码 不起作用 另一个 设置 quot | 更新日期: 2023-09-27 18:27:45
我在asp.net 4.6上使用VS'15。
我还在为我的应用程序建模,在以下位置找到了N层设计:
使用实体框架实现通用数据访问层
我可以从User表返回数据,但是当使用基于User代码的代码从另一个表返回数据时,我会得到null返回。
我的BLL代码工作:
using System;
using System.Collections.Generic;
using System.Linq;
using Library.DataAccessLayer;
using Library.Model;
namespace Library.BusinessLogicLayer
{
public interface IBusinessLogicLayer_User
{
IList<UsersModel> GetAllUsers();
UsersModel GetUserByAapNetUserID(string _UserID);
void UpdateUser(params UsersModel[] _UserModel);
}
public class BusinessLogicLayer_User : IBusinessLogicLayer_User
{
private readonly IUsersRepository _UsersRepository;
public BusinessLogicLayer_User()
{
_UsersRepository = new UsersRepository();
}
public BusinessLogicLayer_User(IUsersRepository userRepository)
{
_UsersRepository = userRepository;
}
public IList<UsersModel> GetAllUsers()
{
return _UsersRepository.GetAll();
}
public UsersModel GetUserByAapNetUserID(string _UserID)
{
return _UsersRepository.GetSingle(u => u.AspNetUserID.Equals(_UserID));
}
public void UpdateUser(params UsersModel[] _UsersModel)
{
_UsersRepository.Update(_UsersModel);
}
}
}
我的BLL代码不起作用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Library.DataAccessLayer;
using Library.Model;
namespace Library.BusinessLogicLayer
{
public interface IBusinessLogicLayer_AreaSecurity
{
AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID);
void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);
void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);
IList<AreaSecurityModel> GetAllAreaSecurity();
}
public class BusinessLogicLayer_AreaSecurity : IBusinessLogicLayer_AreaSecurity
{
private readonly IAreaSecurityRepository _AreaSecurityRepository;
public BusinessLogicLayer_AreaSecurity()
{
_AreaSecurityRepository = new AreaSecurityRepository();
}
public BusinessLogicLayer_AreaSecurity(IAreaSecurityRepository areaSecurityRepository)
{
_AreaSecurityRepository = areaSecurityRepository;
}
public void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
{
_AreaSecurityRepository.Add(_AreaSecurityModel);
}
public IList<AreaSecurityModel> GetAllAreaSecurity()
{
return _AreaSecurityRepository.GetAll();
}
public AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID)
{
return _AreaSecurityRepository.GetSingle(r => r.AspNetUserID.Equals(_AspNetUserID));
}
public void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
{
_AreaSecurityRepository.Update(_AreaSecurityModel);
}
}
}
这是调用有效和无效的东西的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Library.BusinessLogicLayer;
using Library.Model;
namespace IdahoFalls9thWardBulletin.Controllers
{
public class ApplicationBaseController : Controller
{
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (User != null)
{
var context = new ApplicationDbContext();
var username = User.Identity.Name;
if (!string.IsNullOrEmpty(username))
{
var user = context.Users.SingleOrDefault(u => u.UserName == username);
BusinessLogicLayer_User _BusinessLogicLayer_User = new BusinessLogicLayer_User();
var UserDetails = _BusinessLogicLayer_User.GetUserByAapNetUserID(user.Id);
ViewData.Add("FirstName", UserDetails.FirstName);
BusinessLogicLayer_AreaSecurity _BusinessLogicLayer_AreaSecurity = new BusinessLogicLayer_AreaSecurity();
AreaSecurityModel _AreaSecurityModel = _BusinessLogicLayer_AreaSecurity
.GetAllAreaSecurity()
.ToList()
.FirstOrDefault();
ViewData.Add("AreaSecurity", _AreaSecurityModel);
}
}
base.OnActionExecuted(filterContext);
}
public ApplicationBaseController()
{
}
}
}
我不明白的是,当我执行_BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().ToList().FirstOrDefault()
时,它实际上返回了所有记录的列表。
但是,当我执行_BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().GetAreaSecurityByUserID(user.Id)
时,它返回空值。
我不知道我在这里错过了什么。这基本上是我第一次尝试MVC代码优先。
我很惭愧地说,问题是我的安全表数据库被设置为Guid而不是字符串。