Lerning Entity Framework 6 ------ Joins and Left outer Joins
Joins allow developers to combine data from multiple tables into a sigle query.
Let's have a look at codes:
Creating a project
Create a project named JoinTest
Add Packages by NuGet
Create entities:
public class Person
{
public int PersonId { get; set; } [MaxLength(50)]
public string Name { get; set; } public virtual PersonType PersonType { get; set; }
} public class PersonType
{
public int PersonTypeId { get; set; } public string PersonTypeName { get; set; }
} public class MyContext:DbContext
{
public MyContext():base("name=Test")
{ } public DbSet<PersonType> PersonTypes { get; set; } public DbSet<Person> People { get; set; }
}
Execute commands:
- Enable-Migrations
- Add-Migration init
- Update-Database
Add some test data by coding:
static void Main(string[] args)
{
AddTestData();
} private static void AddTestData()
{
using (MyContext context = new MyContext())
{
PersonType student = new PersonType();
student.PersonTypeName = "学生"; PersonType worker = new PersonType();
worker.PersonTypeName = "工人"; Person p1 = new Person();
p1.Name = "王进喜";
p1.PersonType = worker; Person p2 = new Person();
p2.Name = "柴玲";
p2.PersonType = student; Person p3 = new Person();
p3.Name = "完颜亮"; context.People.Add(p1);
context.People.Add(p2);
context.People.Add(p3);
context.SaveChanges();
}
}
}
using joins
static void Main(string[] args)
{
//AddTestData();
using (MyContext db = new MyContext())
{
var result = from p in db.People
join t in db.PersonTypes
on p.PersonType.PersonTypeId equals t.PersonTypeId
select new { Name = p.Name, Type = t.PersonTypeName };
foreach (var item in result)
{
Console.WriteLine(item);
}
}
Console.ReadLine();
}

using Left outer joins
static void Main(string[] args)
{
//AddTestData();
using (MyContext db = new MyContext())
{
var result = from p in db.People
join t in db.PersonTypes
on p.PersonType.PersonTypeId equals t.PersonTypeId into finalGroup
from groupData in finalGroup.DefaultIfEmpty()
select new { Name = p.Name, Type = groupData.PersonTypeName??"Unknown" };
foreach (var item in result)
{
Console.WriteLine(item);
}
}
Console.ReadLine();

I think this tructure is hard to understand, but it's useful.
That's all.
Lerning Entity Framework 6 ------ Joins and Left outer Joins的更多相关文章
- Lerning Entity Framework 6 ------ Defining Relationships
There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...
- Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database
The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...
- Lerning Entity Framework 6 ------ Working with in-memory data
Sometimes, you need to find some data in an existing context instead of the database. By befault, En ...
- Lerning Entity Framework 6 ------ Inserting, Querying, Updating, and Deleting Data
Creating Entities First of all, Let's create some entities to have a test. Create a project Add foll ...
- Lerning Entity Framework 6 ------ Defining the Database Structure
There are three ways to define the database structure by Entity Framework API. They are: Attributes ...
- Lerning Entity Framework 6 ------ Introduction to TPH
Sometimes, you have created two models. They have the same parent class like this: public class Pers ...
- Lerning Entity Framework 6 ------ Complex types
Complex types are classes that map to a subset of columns of a table.They don't contains key. They a ...
- Lerning Entity Framework 6 ------ Using a commandInterceptor
Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...
- Lerning Entity Framework 6 ------ A demo of using Entity framework with MySql
Create a new project named MySqlTest Install following packages by right-clicking on the References ...
随机推荐
- Mac ssh 免密码登录 Mac 或者 Linux
最近在 Mac上操作另一台 Mac 和 Linux 服务器,每次输密码太麻烦.所以直接设置 ssh 免密码登录,省去输入密码的过程.先在本机执行 ls ~/.ssh 若不存在 id_rsa,id_rs ...
- Elasticsearch 整合spring(不是sprig boot)
公司做统计任务,有使用Es做聚合操作,使用的是自己封装的版本,这边整合下原生spring,做下学习记录,随便看一下,发现差不多都是spring boot的案例...我该怎么办,...发现整合的过程其实 ...
- 定时器和函数的使用初级------移动一个div元素
在页面的动画效果中,经常有看到某个小块从一个地方移动到另一个地方的现象,现在,我们也来自己做一个这样的小动画,涉及到的基础包括定时器的使用和函数的使用 例如,我们要实现一个小方块从左面移动到右面,然后 ...
- docker 部署tomcat
使用Docker搭建Tomcat运行环境 1 准备宿主系统 准备一个 CentOS 7操作系统,具体要求如下: 必须是 64 位操作系统 建议内核在 3.8 以上 通过以下命令查看您的 CentOS ...
- 1011 A+B 和 C (15 分)
pragma warning(disable:4996) a-c+b>0? 考虑可能越界 在循环体内判断,然后有序号输出 输入输出格式看多个题然后总结下 不会处理单行数据 include < ...
- 通过scp拷贝文件时无需交互输入密码
工作中经常需要把一些文件从一个服务器传输到另一台服务器,linux环境下最习惯的方式当然是scp,但是scp需要交互输入密码有时候觉得麻烦,记录几种无需手动输入密码的方法. 方法一:建立SSH互信 此 ...
- route命令详解
1. 使用背景:需要接入两个网络,一个是部署环境所在内网环境,这个环境是上不了外网, 外网环境很可能是一个无线网络.如果两者都连接上,很可能导致有一方不能起作用,即外网或内网上不了,常常需要使用繁 ...
- 动态创建js脚本和 css样式
//1.动态添加外部js文件 function loadScript(url){ var script = document.createElement("script"); sc ...
- DJango 基础(6)
Django模型基础 知识点: 数据库的配置 使用django中的模型 将模型映射到数据库 数据的增删改查基本操作 数据库的配置 1.在settings.py中配置DATABASES: DATABAS ...
- Intellij IDEA中maven更新不下来pom中的jar包,reimport失效
问题: Intellij IDEA中使用maven reimport包,一直失败 即使我将本地已存在的一个jar包目录删除了,pom文件那里也没飘红提示找不到 解决方法: maven设置中去掉离线下 ...