【发布时间】:2014-06-22 08:33:41
【问题描述】:
我对存储库模式相当陌生,我有以下项目结构 -
DAL -> 核心 -> 网络
我可以获得 ASP.NET MVC 单独列出所有客户和用户,但是我需要分配给用户的每个用户 (Customers.PrimaryUser) 的客户列表型号。
如何让实体框架中的关系显示在模型中?
DAL
包含实体框架模型
用户服务.cs
public class UserService : ServiceBase<IUserModel>, IUserService
{
public UserService()
: this(new UserRepository())
{
}
private IUserRepository _userRepository;
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository ?? new UserRepository();
}
protected override Type LogPrefix
{
get { return this.GetType(); }
}
public UserListViewModel GetUserList(int PageSize, int CurrentPage)
{
try
{
if ((CurrentPage == 0) || (PageSize == 0))
return null;
IQueryable<User> query = _userRepository.GetQueryable();
UserListViewModel model = new UserListViewModel();
if (model.TotalPageCount != 1)
query = query.OrderBy(x => x.Surname).Skip((CurrentPage - 1) * PageSize).Take(PageSize);
model.UserList = new List<UserModel>();
AutoMapper.Mapper.CreateMap<User, UserModel>()
.ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.ID));
model.UserList = AutoMapper.Mapper.Map(query.ToList(), model.UserList);
return model;
}
catch (System.Exception e)
{
this.LogError("Error getting the user list", e);
return null;
}
}
public UserModel GetSingle(Expression<Func<UserModel, bool>> whereCondition)
{
throw new NotImplementedException();
}
public void Add(UserModel entity)
{
throw new NotImplementedException();
}
public void Delete(UserModel entity)
{
throw new NotImplementedException();
}
public void Update(UserModel entity)
{
throw new NotImplementedException();
}
public IList<UserModel> GetAll(Expression<Func<UserModel, bool>> whereCondition)
{
throw new NotImplementedException();
}
public IList<UserModel> GetAll()
{
throw new NotImplementedException();
}
public IQueryable<UserModel> Query(Expression<Func<UserModel, bool>> whereCondition)
{
throw new NotImplementedException();
}
public long Count(Expression<Func<UserModel, bool>> whereCondition)
{
throw new NotImplementedException();
}
public long Count()
{
throw new NotImplementedException();
}
}
UserModel.cs
public class UserModel : IUserModel
{
private ICustomerService _customerService;
public UserModel()
: this(new CustomerService())
{
}
public UserModel(ICustomerService customerService)
{
_customerService = customerService;
}
public int ID { get; set; }
[DisplayName("Employee Number")]
public string EmployeeNumber { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
public string Position { get; set; }
[DisplayName("Email Address")]
public string Email { get; set; }
public string Password { get; set; }
public UserType UserType { get; set; }
public UserStatus UserStatus { get; set; }
public DateTime DateCreated { get; set; }
public int CreatedBy { get; set; }
public DateTime LastUpdated { get; set; }
public int LastUpdateBy { get; set; }
[DisplayName("Full Name")]
public string SurnameFirstName
{
get { return Surname + ", " + Firstname; }
}
}
CustomerModel.cs
public class CustomerModel
{
public int ID { get; set; }
public string CompanyName { get; set; }
public int BillingAddress { get; set; }
public string Notes { get; set; }
public int CustomerType { get; set; }
public int RefSource { get; set; }
public DateTime RefDate { get; set; }
public string RefPerson { get; set; }
public int RefType { get; set; }
public string RefNotes { get; set; }
public int PrimaryUser { get; set; }
public DateTime DateCreated { get; set; }
public int CreatedBy { get; set; }
public DateTime LastUpdated { get; set; }
public int LastUpdateBy { get; set; }
}
【问题讨论】:
-
代码太多了..你能把它缩小到你遇到问题的具体部分
-
这已被编辑 - 现在显示用户服务和用户/客户模型。关系应该是一个用户有很多客户 (Customer.PrimaryUser)。
标签: c# asp.net-mvc entity-framework asp.net-mvc-4 repository-pattern