PDA

View Full Version : معادل دستور SQL در هنگام استفاده از First code EF5



naeeme
یک شنبه 20 بهمن 1392, 14:17 عصر
با کمک First code EF5 یک دیتابیس ایجاد کرده ام. در این دیتابیس چندین رابطه many-tp-many وجود داره. برای دریافت اطلاعات مربوط به این رابطه ها به مشکل برخورد.

کلاسها به صورت زیر تعریف شده اند:

public abstract class BaseEntity
{
public int Id { get; set; }
}

public class UserGroupEntity : BaseEntity
{
public UserGroupEntity()
{
this.Users = new List<UserEntity>();
this.Exams = new List<ExamEntity>();
}

public string Name { get; set; }
public byte State { get; set; }

public virtual IList<UserEntity> Users { get; set; }
public virtual IList<ExamEntity> Exams { get; set; }
}

public class UserEntity : BaseEntity
{
public UserEntity()
{
UserGroups = new List<UserGroupEntity>();
}

public string UserName { get; set; }
public byte State { get; set; }

public virtual IList<UserGroupEntity> UserGroups { get; set; }
}

public class ExamGroupEntity : BaseEntity
{
public ExamGroupEntity()
{
this.Exams = new List<ExamEntity>();
}

public string Name { get; set; }
public byte State { get; set; }

public virtual IList<ExamEntity> Exams { get; set; }
}

public class ExamEntity : BaseEntity
{
public ExamEntity()
{
this.UserGroups = new List<UserGroupEntity>();
this.ExamDetail = new List<ExamDetailEntity>();
}

public string Name { get; set; }
public byte State { get; set; }

public virtual ExamGroupEntity ExamGroup { get; set; }
public virtual IList<UserGroupEntity> UserGroups { get; set; }
}


و رابطه ها به صورت زیر تعریف شده اند:

public class UserMapping : BaseMapping<UserEntity>
{
public UserMapping()
: base("Users")
{
// Many2Many
this.HasMany(x => x.UserGroups)
.WithMany(x => x.Users)
.Map(map =>
{
map.MapLeftKey("UserId");
map.MapRightKey("UserGroupId");
map.ToTable("UsersJoinUserGroups");
});
}
}
public class ExamMapping : BaseMapping<ExamEntity>
{
public ExamMapping()
: base()
{
// Many2Many
this.HasMany(x => x.UserGroups)
.WithMany(x => x.Exams)
.Map(map =>
{
map.MapLeftKey("ExamId");
map.MapRightKey("UserGroupId");
map.ToTable("ExamsJoinUserGroups");
});
}
}


با توجه به این رابطه ها، دستور معادل این دستور SQL چه خواهد بود؟

select e.*
from Exams e join ExamsJoinUserGroups EJU
on e.Id= eju.ExamId join UsersJoinUserGroups UJU
on EJU.UserGroupId = UJU.UserGroupId
where UJU.UserId=2
and e.ExamGroup_Id=1
and e.State=1

Mahmoud.Afrad
دوشنبه 21 بهمن 1392, 01:29 صبح
سرچ میکردی قبلا تاپیک در مورد join ایجاد و پاسخ داده شده:


var query = from exam in db.Exams
join eju in db.ExamsJoinUserGroups on exam.id equals eju.ExamId
join uju in db.UsersJoinUserGroups on eju.UserGroupId equals uju.UserGroupId
where uju.UserId == 2 && exam.ExamGroup_Id = 1 && exam.State = 1
select exam;

naeeme
چهارشنبه 23 بهمن 1392, 16:36 عصر
نه دیگه! من براساس مدل طراحی شده میخواستم و در مدل من معادل UsersJoinUserGroups وجود نداره. چون این جدول رو EF خودش می سازه و مدیریت میکنه. پس توی دستور ما نمیاد.

پاسخ درست به صورت زیر هست.

var exams = context.ExamEntities
.Where(e => e.UserGroups.Any(ug => ug.Users.Any(u => u.Id == 2)) &&
e.ExamGroup.Id == 1 &&
e.State == 1)
.ToList();