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
کلاسها به صورت زیر تعریف شده اند:
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