EF Core » 影子属性
Caution:注意
This documentation is for EF Core. For EF6.x and earlier release see http://msdn.com/data/ef.
该文本用于EF Core。EF6.x和更早的版本请参看:http:msdn.com/data/ef。
Shadow Properties¶
Shadow properties are properties that do not exist in your entity class. The value and state of these properties is maintained purely in the Change Tracker.
影子属性是这样的属性,它并不存在于你的实体类中。它的值和状态纯粹在变更跟踪器?中维护。
Shadow property values can be obtained and changed through the ChangeTracker
API.
影子属性的值可通过ChangeTracker API获得和改变。
context.Entry(myBlog).Property("LastUpdated").CurrentValue = DateTime.Now;
Shadow properties can be referenced in LINQ queries via the EF.Property
static method.
影子属性通过EF.Property静态方法在LINQ查询中引用。
var blogs = context.Blogs
.OrderBy(b => EF.Property<DateTime>(b, "LastUpdated"));
In this article:
- Shadow Properties 影子属性
- Conventions 习惯
- Data Annotations 数据注释
- Fluent API Fluent API
Conventions¶ 习惯
By convention, shadow properties are only created when a relationship is discovered but no foreign key property is found in the dependent entity class. In this case, a shadow foreign key property will be introduced. The shadow foreign key property will be named <navigation property name><principal key property name>
(the navigation on the dependent entity, which points to the principal entity, is used for the naming). If the principal key property name includes the name of the navigation property, then the name will just be <principal key property name>
. If there is no navigation property on the dependent entity, then the principal type name is used in its place.
习惯上,当有一个关系在依赖的实体类中没有外键属性时,才设置影子属性。在这种情况下,应该引入影子外键属性。影子外键属性被命名为<navigation property name><principal key property name> (引入实体的导航指向基础实体,被用于命名)。如果princepal key属性名称包括导航属性,这样名称就成了<principal key property name>。 如果在引入实体中没有导航属性,这样???
For example, the following code listing will result in a BlogId
shadow property being introduced to the Post
entity.
例如,下列代码BlogId影子属性引入到Post实体中。
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
} public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } public List<Post> Posts { get; set; }
} public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; } public Blog Blog { get; set; }
}
Data Annotations¶
Shadow properties can not be created with data annotations.影子属性可以通过数据声明来建立。
Fluent API¶
You can use the Fluent API to configure shadow properties. Once you have called the string overload of Property
you can chain any of the configuration calls you would for other properties.
If the name supplied to the Property
method matches the name of an existing property (a shadow property or one defined on the entity class), then the code will configure that existing property rather than introducing a new shadow property.
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property<DateTime>("LastUpdated");
}
} public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
EF Core » 影子属性的更多相关文章
- 【ASP.NET Core】EF Core - “影子属性” 深入浅出经典面试题:从浏览器中输入URL到页面加载发生了什么 - Part 1
[ASP.NET Core]EF Core - “影子属性” 有朋友说老周近来博客更新较慢,确实有些慢,因为有些 bug 要研究,另外就是老周把部分内容转到直播上面,所以写博客的内容减少了一点. ...
- 【ASP.NET Core】EF Core - “影子属性”
有朋友说老周近来博客更新较慢,确实有些慢,因为有些 bug 要研究,另外就是老周把部分内容转到直播上面,所以写博客的内容减少了一点. 老周觉得,视频直播可能会好一些,虽然我的水平一般,不过直播时,老周 ...
- C# 数据操作系列 - 7. EF Core 导航属性配置
在上一篇,大概介绍了Entity Framework Core关于关系映射的逻辑.在上一篇中留下了EF的外键映射没有说,也就是一对一,一对多,多对一,多对多的关系等.这一篇将为大家细细分析一下,如何设 ...
- 【ASP.NET Core】EF Core - “导航属性”
“导航属性”是实体框架用得算是比较频繁的概念. 首先,它是类型成员,其次,他是属性,这不是 F 话,而是明确它的本质.那么,什么场景下会用到导航属性呢?重点就落在“导航”一词上了,当实体 A 需要引用 ...
- C# 数据操作系列 - 8. EF Core的增删改查
0.前言 到目前为止,我们看了一下如何声明EF Core的初步使用,也整体的看了下EF Core的映射关系配置以及导航属性的配置. 这一篇,我带大家分享一下,我在工作中需要的EF Core的用法. 1 ...
- EF Core中Key属性相同的实体只能被跟踪(track)一次
在EF Core的DbContext中,我们可以通过DbContext或DbSet的Attach方法,来让DbContext上下文来跟踪(track)一个实体对象,假设现在我们有User实体对象,其U ...
- EF Core中怎么实现自动更新实体的属性值到数据库
我们在开发系统的时候,经常会遇到这种需求数据库表中的行被更新时需要自动更新某些列. 数据库 比如下面的Person表有一列UpdateTime,这列数据要求在行被更新后自动更新为系统的当前时间. Pe ...
- EF Core中如何通过实体集合属性删除从表的数据
假设在数据库中有两个表:Person表和Book表,Person和Book是一对多关系 Person表数据: Book表数据: 可以看到数据库Book表中所有的数据都属于Person表中"F ...
- EF Core反向导航属性解决多对一关系
多对一是一种很常见的关系,例如:一个班级有一个学生集合属性,同时,班级有班长.语文课代表.数学课代表等单个学生属性,如果定义2个实体类,班级SchoolClass和学生Student,那么,班级Sch ...
随机推荐
- 监控windows服务,当服务停止后自动重启服务
近期花时间研究了一下windows和linux下某服务停了后自动重启的功能,在网上收集了些资料,并经过测试,在此整理一下.这里介绍的是windows服务的监控,是通过批处理来实现的.本例是监控wind ...
- 没有必要去指定SqlSessionFactory或SqlSessionTemplate
<!-- 自动注册mybatis mapper bean --><!-- 注意,没有必要去指定SqlSessionFactory或SqlSessionTemplate, 因为 ...
- DISPLAY_ITEM built-in in Oracle D2k Forms
DISPLAY_ITEM built-in in Oracle D2k Forms DescriptionMaintained for backward compatibility only. For ...
- Hibernate+Struts2+jsp 修改用户信息
在用户列表页面点击修改,进入修改页面 修改薪酬为555,点击提交,重新跳回该页面 修改成功 关键代码如下 基层的代码,这里增加了一个根据用户id查询的方法 dao层 //修改 public USer ...
- firefox渗透师必备的利器
工欲善必先利其器,firefox一直是各位渗透师必备的利器,小编这里推荐34款firefox渗透测试辅助插件,其中包含渗透测试.信息收集.代理.加密解密等功能. 1:Firebug Firefox的 ...
- C# 导出数据至 CSV
有时候将Excel的数据另存到csv文件会出现csv格式错误,以下示例实现将DataTable里面的数据直接保存到csv文件. System.Web.HttpRuntime.Cache["v ...
- FJNU 1159 Fat Brother’s new way(胖哥的新姿势)
FJNU 1159 Fat Brother’s new way(胖哥的新姿势) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目 ...
- getting started with building a ROS simulation platform for Deep Reinforcement Learning
Apparently, this ongoing work is to make a preparation for futural research on Deep Reinforcement Le ...
- iOS - Swift iOS 开发体系
1.iOS 开发技术体系 iOS 开发技术体系图: 层级 主要框架 Cocoa Touch UIKit 等 Media Core Graphics .OpenGl ES.Core Animation ...
- mysql概要(十一)存储引擎
1.数据库对同样的数据可以不同的方式存储和管理,每种方式对应一种引擎. 1.1定义: 2.引擎种类的特点: