Yes, using Include also we can retrive the relavent data using Navigation Property,but which excepts a navigation property. like
public class Employee
{
[Key]
public int Eid { get; set; }
public string Ename {
get; set; }
public string EPhone
{ get; set; }
public int EmpDept { get; set; }
public virtual Departments Dept { get;
set; } // Navigation Property
}
public class Departments
{
[Key]
public int Did { get; set; }
public string DName {
get; set;
}
}
public class Conn : DbContext
{
public DbSet<Employee> emps { get;
set; }
public DbSet<Departments> depts { get;
set; }
protected override void OnModelCreating(DbModelBuilder
modelBuilder)
{
modelBuilder.Entity<Employee>()
.HasRequired(a => a.Dept)
.WithMany()
.HasForeignKey(u => u.EmpDept);
base.OnModelCreating(modelBuilder);
}
}
then
-----
class Program
{
static void Main(string[] args)
{
Conn context = new
Conn();
Employee eobj = new
Employee { Ename = "France",
EPhone = "7176", EmpDept = 2 };
context.emps.Add(eobj);
context.SaveChanges();
}
}
thin Executed in DAtabase
here where is department details?
so by default lazyloading is enabled but as per quering the data base only which returns now.
which will generates.
as well as
now we can insert multiple employee records
as well as
we can retrive multiple employee records also. as a collections
how to insert multiple records:
how to retrive particular dept records :
Columns:
EmpDept----particular employee
thin Executed in DAtabase
Eid Ename EPhone EmpDept Did DName
1 raju 7171 1 1 Accounts
2 naveen 7172 2 2 Business
3 Chaitanya 7173 1 1 Accounts
4 Dinesh 7174 2 2 Business
5 Eswar 7175 3 3 DBA
6 France 7176 2 2 Business
emps------------> select * from emp
IQueryable<Employee>
t = context.emps.Include("Dept").Where(n
=> n.Ename == "raju").Select(n
=> n);
immediate window
t
SELECT
[Extent1].[Eid] AS [Eid],
[Extent1].[Ename] AS [Ename],
[Extent1].[EPhone] AS [EPhone],
[Extent1].[EmpDept] AS [EmpDept],
[Extent2].[Did] AS [Did],
[Extent2].[DName] AS [DName]
FROM [dbo].[Employees] AS [Extent1]
INNER JOIN [dbo].[Departments] AS [Extent2] ON [Extent1].[EmpDept] = [Extent2].[Did]
WHERE N'raju' = [Extent1].[Ename]
Eid Ename EPhone EmpDept Did DName
1 raju 7171 1 1 Accounts
6 raju 7176 2 2 Business
here where is department details?
so by default lazyloading is enabled but as per quering the data base only which returns now.
which will generates.
as well as
public class Departments
{
[Key]
public int Did { get; set; }
public string DName {
get; set; }
public virtual ICollection<Employee> DeptEmployees { get; set; }
}
now we can insert multiple employee records
as well as
we can retrive multiple employee records also. as a collections
how to insert multiple records:
Employee emp = new
Employee { Ename = "narayanarao",
EPhone = "7175" };
Employee emp1=new Employee{ Ename="shekar", EPhone="7176"};
List<Employee>
elist=new List<Employee>();
elist.Add(emp);
elist.Add(emp1);
Departments
d = new Departments
{ DeptEmployees = elist, DName="Professionals"
};
context.depts.Add(d);
context.SaveChanges();
var t = context.depts.Include("DeptEmployees"); // DeptEmployees
foreach (var
item in t)
{
ICollection<Employee> ems = item.DeptEmployees; // grouped by
department id
foreach (var
items in ems)
{
Console.WriteLine(items.Ename);
Console.WriteLine(item.DName);
}
}
Note: DeptEmployees column is created at Employee table why because to this column referenceing the apporiate Employee.
Columns:
EmpDept----particular employee
Departments_Did-----collection of employees/ a particular dept employees
The Best Mobile Casinos - JTG Hub
ReplyDeleteIt's not just 광주 출장샵 gambling, there's 김제 출장샵 tons of 거제 출장마사지 fun. From slots and table 속초 출장마사지 games to video poker and other casino games, there's always the best place to 경기도 출장샵 play at