博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework Code First添加修改及删除外键关联实体
阅读量:4951 次
发布时间:2019-06-11

本文共 6429 字,大约阅读时间需要 21 分钟。

1、添加外键关联实体

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

using (var ctx = new PortalContext()){    var city1 = new City    {        CityNo = "10010",        CityName = "测试城市1"    };    var city2 = new City    {        CityNo = "10020",        CityName = "测试城市2"    };    var province = new Province    {        ProvinceNo = "10000",        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'10000',@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'10010',@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'10020',@2=N'测试城市2'

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

using (var ctx = new PortalContext()){    var city1 = new City    {        CityNo = "10010",        CityName = "测试城市1"    };    var city2 = new City    {        CityNo = "10020",        CityName = "测试城市2"    };    var city3 = ctx.Cities.Find(36);    var province = new Province    {        ProvinceNo = "10000",        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'10000',@1=N'测试省份'
exec sp_executesql N'update [dbo].[City]set [ProvinceID] = @0where ([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'10010',@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'10020',@2=N'测试城市2'

  2、修改外键关联实体

  1>、方式1

using (var ctx = new PortalContext()){    var city = ctx.Cities.Find(40);    var province = ctx.Provinces.Find(10);    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] = @0where ([CityID] = @1)',N'@0 int,@1 int',@0=10,@1=40

  2>、方式2

using (var ctx = new PortalContext()){    var city = ctx.Cities.Find(41);    var province = ctx.Provinces.Find(10);    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(42);    var province = ctx.Provinces.Find(10);    city.ProvinceID = province.ProvinceID;    ctx.SaveChanges();}

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

  3、删除外键关联实体

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

using (var ctx = new PortalContext()){    var city = ctx.Cities.Find(42);    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] = nullwhere ([CityID] = @0)',N'@0 int',@0=42

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

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

  

原文链接:

转载于:https://www.cnblogs.com/1175429393wljblog/p/10778536.html

你可能感兴趣的文章
【Hibernate学习笔记-5】@Formula注解的使用
查看>>
链接元素<a>
查看>>
Binding object to winForm controller through VS2010 Designer(通过VS2010设计器将对象绑定到winForm控件上)...
查看>>
Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
查看>>
前端性能优化集【持续更新】
查看>>
第二章:webdriver 控制浏览器窗口大小
查看>>
四则运算2初步构思
查看>>
Break the Chocolate(规律)
查看>>
C#jbox小节
查看>>
结构体指针释放的问题
查看>>
C#枚举Enum[轉]
查看>>
第三百五十七天 how can I 坚持
查看>>
【动态规划】流水作业调度问题与Johnson法则
查看>>
startActivityForResult不起作用
查看>>
Python&Selenium&Unittest&BeautifuReport 自动化测试并生成HTML自动化测试报告
查看>>
活现被翻转生命
查看>>
POJ 1228
查看>>
SwaggerUI+SpringMVC——构建RestFul API的可视化界面
查看>>
springmvc怎么在启动时自己执行一个线程
查看>>
流操作的规律
查看>>