我在EF中的实体不与其他与它们有关系的实体连接

时间:2014-06-14 15:41:35

标签: c# entity-framework model-view-controller

我使用EF6设计我的模型。我有2个实体用户公司,用户可以在系统中注册1个或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}
}

它不起作用,即这部分(如果条件) id == compnayid 无法解决?!!!为什么?

1 个答案:

答案 0 :(得分:2)

用户拥有公司集合,您无法使用点访问公司的财产,因此您必须更改查询表单

objUserRepository.FindBy(i=>i.Id==id & i.Companies.id==compnayid).Count()==1)

objUserRepository.FindBy(i=>i.Id==id && i.Companies.Any(c => c.id==compnayid)).Count()==1)