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 ...
随机推荐
- 为什么SQL用UPDATE语句更新时更新行数会多3行有触发器有触发器有触发器有触发器有触发器有触发器
update明显更新就一行,但是结果显示更新多行. 原因是有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器
- JavaScript倒计时实现
/** * 倒计时函数 * @param {String}} endTime 终止时间戳 */ const countDown = (endTime, callback) => { const ...
- Asp.net之Sql注入与Parameter对象
一.Sql注入实例 using System; using System.Collections.Generic; using System.Data; using System.Data.SqlCl ...
- as3:sprite作为容器使用时,最好不要指定width,height
除 TextField 和 Video 对象以外,没有内容的显示对象(如一个空的 Sprite)的高度为 0,即使您尝试将 height 设置为其它值,也是这样. 如果您设置了 height 属性,则 ...
- ASP.NET Boilerplate-AbpSession
/------2016-05-15/------介绍 如果一个应用支持登录,也许需要知道当前登录用户的一些操作,然而ASP.NET 本身对于展现层提供了Session的支持,ABP提供了 IAbpSe ...
- oracle 新增并返回新增的主键
oracle 的insert into 语句需要返回新增的主键的时候,可以使用一下insert 语法: insert into ims.t_bank_inquire_results (t_date,l ...
- [leetcode]156.Binary Tree Upside Down颠倒二叉树
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- js学习(4) 函数
JavaScript有三种声明函数的方法 (1)function命令 function print(s) { console.log(s); } (2)函数表达式 1.var print = func ...
- stark组件开发之排序
class StartHandler(object): .......... ordered_list = [] # 排序规则由 用户指定. def get_ordered_list(self): r ...
- (转)android import library switch语句报错case expressions must be constant expressions
今天当我从github上下载一个工程,并把它的库文件导入eclipse中,发现switch语句报错case expressions must be constant expressions : 解决方 ...