【发布时间】:2014-06-14 15:41:34
【问题描述】:
我使用 EF6 设计我的模型。我有 2 个实体 User 和 Company,用户可以在系统中注册 1 个或多个公司我的实体是这样的:
public partial class User
{
public User()
{
this.Ideas = new HashSet<Idea>();
this.Companies = new HashSet<Company>();
}
public int Id { get; set; }
[DisplayName("نام")]
[Required(ErrorMessage = "را وارد کنید")]
public string Name { get; set; }
[DisplayName("نام خانوادگی")]
[Required(ErrorMessage = "را وارد کنید")]
public string Family { get; set; }
[DisplayName("تحصیلات")]
[Required(ErrorMessage = "را انتخاب کنید")]
public string EducationLevel { get; set; }
[DisplayName("دانشگاه")]
[Required(ErrorMessage = "را وارد کنید")]
public string University { get; set; }
[DisplayName("کد ملی")]
[StringLength(10, ErrorMessage = "باید ده رقمی باشد", MinimumLength = 10)]
[Required(ErrorMessage = "را وارد نمایید")]
public string IntCode { get; set; }
[DisplayName("سطح دسترسی")]
public string Permission { get; set; }
[DisplayName("کلمه عبور")]
[Required(ErrorMessage = "را وارد نمایید")]
[DataType(DataType.Password)]
public string Password { get; set; }
[DisplayName("تاریخ ثبت نام")]
public Nullable<System.DateTime> Date { get; set; }
[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public virtual ICollection<Idea> Ideas { get; set; }
public virtual ICollection<Company> Companies { get; set; }
}
还有我的公司实体:
public partial class Company
{
public Company()
{
this.CompanyMembers = new HashSet<CompanyMember>();
this.Activities = new HashSet<Activity>();
this.Attachments = new HashSet<Attachment>();
this.Publications = new HashSet<Publication>();
}
[DisplayName("شناسه")]
public int Id { get; set; }
[DisplayName(" نام شرکت")]
[Required(ErrorMessage = "را وارد نمایید")]
public string Name { get; set; }
[DisplayName("آدرس وب سایت شرکت")]
[Required(ErrorMessage = "را وارد نمایید")]
public string Website { get; set; }
public int UserId { get; set; }
public virtual ICollection<CompanyMember> CompanyMembers { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
public virtual ICollection<Attachment> Attachments { get; set; }
public virtual ICollection<Publication> Publications { get; set; }
}
这两个实体在 UserId 列上有关系。我为我的存储库写了一个摘要,你可以在这里看到:
public abstract class GenericRepository<C, T> :
IGenericRepository<T>
where T : class
where C : DbContext, new()
{
private C _entities = new C();
public C Context
{
get { return _entities; }
set { _entities = value; }
}
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _entities.Set<T>();
return query;
}
public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _entities.Set<T>().Where(predicate);
return query;
}}
所以我创建了一个 userrepository :
namespace Repository
{
public class userRepository:GenericRepository<InModelContainer,User>
{
}}
现在我的问题是,我想写一个这样的查询:
public ActionResult Index(int companyId)
{
int id = objUserRepository.FindBy(i => i.Email == User.Identity.Name).First().Id;
if( objUserRepository.FindBy(i=>i.Id==id & i.Companies.id==compnayid).Count()==1){do something}
}
它不起作用,即这部分(在 if 条件下)id==compnayid 无法解决?!!!为什么?
【问题讨论】:
-
您是否有意使用二进制 & 而不是 &&?
-
不,我只想检查我的 find 子句中的两个条件!
标签: c# entity-framework model-view-controller