1、添加外键关联实体

  1>、添加新的Province及City实体

using (var ctx = new PortalContext())
{
var city1 = new City
{
CityNo = "",
CityName = "测试城市1"
};
var city2 = new City
{
CityNo = "",
CityName = "测试城市2"
}; var province = new Province
{
ProvinceNo = "",
ProvinceName = "测试省份"
};
province.Cities.Add(city1);
province.Cities.Add(city2);
ctx.Provinces.Add(province); ctx.SaveChanges();
}

  代码运行所执行的SQL语句:

exec sp_executesql N'insert [dbo].[Province]([ProvinceNo], [ProvinceName])
values (@0, @1)
select [ProvinceID]
from [dbo].[Province]
where @@ROWCOUNT > 0 and [ProvinceID] = scope_identity()',N'@0 nvarchar(10),@1 nvarchar(50)',@0=N'',@1=N'测试省份'
exec sp_executesql N'insert [dbo].[City]([ProvinceID], [CityNo], [CityName])
values (@0, @1, @2)
select [CityID]
from [dbo].[City]
where @@ROWCOUNT > 0 and [CityID] = scope_identity()',N'@0 int,@1 nvarchar(10),@2 nvarchar(50)',@0=39,@1=N'',@2=N'测试城市1'
exec sp_executesql N'insert [dbo].[City]([ProvinceID], [CityNo], [CityName])
values (@0, @1, @2)
select [CityID]
from [dbo].[City]
where @@ROWCOUNT > 0 and [CityID] = scope_identity()',N'@0 int,@1 nvarchar(10),@2 nvarchar(50)',@0=39,@1=N'',@2=N'测试城市2'

  2>、添加新的Province实体及现有的City实体

using (var ctx = new PortalContext())
{
var city1 = new City
{
CityNo = "",
CityName = "测试城市1"
};
var city2 = new City
{
CityNo = "",
CityName = "测试城市2"
};
var city3 = ctx.Cities.Find(); var province = new Province
{
ProvinceNo = "",
ProvinceName = "测试省份"
};
province.Cities.Add(city1);
province.Cities.Add(city2);
province.Cities.Add(city3);
ctx.Provinces.Add(province); ctx.SaveChanges();
}

  代码运行所执行的SQL语句:

exec sp_executesql N'SELECT
[Limit1].[CityID] AS [CityID],
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[CityNo] AS [CityNo],
[Limit1].[CityName] AS [CityName]
FROM ( SELECT TOP (2)
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE [Extent1].[CityID] = @p0
) AS [Limit1]',N'@p0 int',@p0=36
exec sp_executesql N'insert [dbo].[Province]([ProvinceNo], [ProvinceName])
values (@0, @1)
select [ProvinceID]
from [dbo].[Province]
where @@ROWCOUNT > 0 and [ProvinceID] = scope_identity()',N'@0 nvarchar(10),@1 nvarchar(50)',@0=N'',@1=N'测试省份'
exec sp_executesql N'update [dbo].[City]
set [ProvinceID] = @0
where ([CityID] = @1)
',N'@0 int,@1 int',@0=40,@1=36
exec sp_executesql N'insert [dbo].[City]([ProvinceID], [CityNo], [CityName])
values (@0, @1, @2)
select [CityID]
from [dbo].[City]
where @@ROWCOUNT > 0 and [CityID] = scope_identity()',N'@0 int,@1 nvarchar(10),@2 nvarchar(50)',@0=40,@1=N'',@2=N'测试城市1'
exec sp_executesql N'insert [dbo].[City]([ProvinceID], [CityNo], [CityName])
values (@0, @1, @2)
select [CityID]
from [dbo].[City]
where @@ROWCOUNT > 0 and [CityID] = scope_identity()',N'@0 int,@1 nvarchar(10),@2 nvarchar(50)',@0=40,@1=N'',@2=N'测试城市2'

  2、修改外键关联实体

  1>、方式1

using (var ctx = new PortalContext())
{
var city = ctx.Cities.Find();
var province = ctx.Provinces.Find();
city.Province = province; ctx.SaveChanges();
}

  代码运行所执行的SQL语句:

exec sp_executesql N'SELECT
[Limit1].[CityID] AS [CityID],
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[CityNo] AS [CityNo],
[Limit1].[CityName] AS [CityName]
FROM ( SELECT TOP (2)
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE [Extent1].[CityID] = @p0
) AS [Limit1]',N'@p0 int',@p0=40
exec sp_executesql N'SELECT
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[ProvinceNo] AS [ProvinceNo],
[Limit1].[ProvinceName] AS [ProvinceName]
FROM ( SELECT TOP (2)
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[ProvinceNo] AS [ProvinceNo],
[Extent1].[ProvinceName] AS [ProvinceName]
FROM [dbo].[Province] AS [Extent1]
WHERE [Extent1].[ProvinceID] = @p0
) AS [Limit1]',N'@p0 int',@p0=10
exec sp_executesql N'update [dbo].[City]
set [ProvinceID] = @0
where ([CityID] = @1)
',N'@0 int,@1 int',@0=10,@1=40

  2>、方式2

using (var ctx = new PortalContext())
{
var city = ctx.Cities.Find();
var province = ctx.Provinces.Find();
province.Cities.Add(city); ctx.SaveChanges();
}

  方式2的实现方式与方式1的实现方式区别:在执行province.Cities.Add(city)时,会自动调用延迟加载,多执行一次从数据库中根据Province关联获取City的SQL语句。

exec sp_executesql N'SELECT
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE [Extent1].[ProvinceID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=10

  3>、方式3

using (var ctx = new PortalContext())
{
var city = ctx.Cities.Find();
var province = ctx.Provinces.Find();
city.ProvinceID = province.ProvinceID; ctx.SaveChanges();
}

  方式3与方式1在代码运行之后所执行的SQL语句是一样的。

  3、删除外键关联实体

  示例:对外键允许为空的外键表记录删除引用,在City表中外键ProvinceID引用Province表,并允许为空。

using (var ctx = new PortalContext())
{
var city = ctx.Cities.Find();
ctx.Entry(city)
.Reference(c => c.Province)
.Load();
city.Province = null; ctx.SaveChanges();
}

  代码运行所执行的SQL语句:

exec sp_executesql N'SELECT
[Limit1].[CityID] AS [CityID],
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[CityNo] AS [CityNo],
[Limit1].[CityName] AS [CityName]
FROM ( SELECT TOP (2)
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE [Extent1].[CityID] = @p0
) AS [Limit1]',N'@p0 int',@p0=42
exec sp_executesql N'SELECT
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[ProvinceNo] AS [ProvinceNo],
[Extent1].[ProvinceName] AS [ProvinceName]
FROM [dbo].[Province] AS [Extent1]
WHERE [Extent1].[ProvinceID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=10
exec sp_executesql N'update [dbo].[City]
set [ProvinceID] = null
where ([CityID] = @0)
',N'@0 int',@0=42

  根据外键删除与主键表的关联引用的另外一种实现方式:

using (var ctx = new PortalContext())
{
var city = ctx.Cities.Find();
ctx.Entry(city)
.Reference(c => c.Province)
.Load();
city.ProvinceID = null; ctx.SaveChanges();
}

Entity Framework Code First添加修改及删除外键关联实体的更多相关文章

  1. Entity Framework Code First添加修改及删除单独实体

    对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...

  2. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  3. Entity Framework Code First学习系列

    Entity Framework Code First学习系列目录 Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity ...

  4. Entity Framework Code First关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  5. Entity Framework Code First主外键关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  6. Entity Framework Code First关系映射约定【l转发】

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  7. Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  8. 补习知识:Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  9. Entity Framework Code First属性映射约定 转载https://www.cnblogs.com/libingql/p/3352058.html

    Entity Framework Code First属性映射约定   Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Flue ...

随机推荐

  1. Hololens 手势事件执行顺序

    InteractionManager_SourcePressed (Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runt ...

  2. 前馈网络求导概论(一)·Softmax篇

    Softmax是啥? Hopfield网络的能量观点 1982年的Hopfiled网络首次将统计物理学的能量观点引入到神经网络中, 将神经网络的全局最小值求解,近似认为是求解热力学系统的能量最低点(最 ...

  3. SqlServer windowss身份登陆和sa身份登陆

    今天重新装了系统,但是计算机名变了,于是修改了计算机名,然后装了SQLSEVER,安装完成后登录,发现无论用WINDOWS身份还是SQLSERVER身份都登录不了 1.先说说sqlserver身份登录 ...

  4. weex image

    weex 的image用来渲染图片, 可以使用img作为它的别名. 需要注意的是,他的长度可宽度必须指定, 不然它是不会工作的. 它没有任何的子组件. 有两个属性: src 用来指定图片的地址图片. ...

  5. Flex DataGrid可编辑对象实现Enter跳转

    来源:http://blog.sina.com.cn/s/blog_5ed17a730100vrja.html 在Flex DataGrid中实现点击Enter键可编辑对象跳转<?xml ver ...

  6. js判断undefined类型

    js判断undefined类型 if (reValue== undefined){    alert("undefined");    }  发现判断不出来,最后查了下资料要用ty ...

  7. 转:CentOS, 找不到dump命令:command not found

    dump 功能说明:备份文件系统.语 法:dump [-cnu][-0123456789][-b <区块大小>][-B <区块数目>][-d <密度>][-f &l ...

  8. Xcode 改时间问题 lua代码没反应问题

    Xcode 改时间问题  rm -fr $_TARGET_BUILD_CONTENTS_PATH/$1/*

  9. ThinkPHP5 助手函数

    对于ThinkPHP5.0以前的版本,助手函数全部是单字母函数,但到ThinkPHP5之后,使用如下函数来代替单字母函数: 最常用: /** * 实例化Model * @param string $n ...

  10. SSH配置与讲解

    一.Struts 首先介绍Struts,在web项目中加入Struts的jar包,并在Web.xml中添加Struts的配置: <filter>       <filter-name ...