I am using Database first approach in Entity framework. I have two tables i.e. Person and Role table. Person table has 3 columns
- PersonId
- Name
- RoleId
Role table has 2 columns
- RoleId
- RoleName
The automated generated classes for these two tables are below along with the foreign key relationship.
public partial class Role
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Role()
{
this.People = new HashSet<Person>();
}
public int RoleId { get; set; }
public string RoleName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Person> People { get; set; }
}
public partial class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public virtual Role Role { get; set; }
}
I used following code to retrieve the data from Role table
public List<Role> GetRole()
{
var _context= new TestDBEntities();
List<Role> data = new List<Role>();
data = _context.Roles.ToList();
return data;
}
While retrieving the data from the Role table, all the data from the Role table was retrieved but the Person table data was also retrieved along with it. The reason behind it must be the relationship between these two tables. So, I want to fetch the data only from the Role table. Can anyone assist me on how to fetch the data from the Role table only without the data from Person?
public TestDBEntities() : base("name=TestDBEntities") { this.Configuration.LazyLoadingEnabled = false; }