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 ...
随机推荐
- ViewState
ViewState就像一个记录本,由于WebFormd的无状态性,刷新了页面.那么这个页面就和上一个页面没有任何关系了.为了使刷新前的页面和本页面产生联系,ViewState的作用就是记录刷新前页面的 ...
- Linux下mysql主从配置
mysql服务器的主从配置,这样可以实现读写分离,也可以在主库挂掉后从备用库中恢复需要两台机器,安装mysql,两台机器要在相通的局域网内主机A: 192.168.1.100从机B:192.168.1 ...
- ios项目中引用其他项目复习
ios项目中引用其他开源项目,今天再次复习了,记个备注. 1. 将开源项目的.xcodeproj拖入项目frameworks 2. Build Phases下 Links Binary With Li ...
- 模块"xxxx.dll"已加载,但对DllRegisterServer的调用失败,错误代码为 XXXXXXXXX
WIN7.WIN8 注册 卸载dll 报错: 模块"xxxx.dll"已加载,但对DllRegisterServer的调用失败,错误代码为 XXXXXXXXX 解决方法: 若为 ...
- adb命令大全「含shell和wait-for-devices等」
adb shell 大全: http://adbshell.com/commands 下列表格列出了adb常见命令,注意,它并不是只有adb shell,shell只是其中一个. Category C ...
- SQL事务回滚样例
选课系统,当同意学号选课数量超过则回滚事务,符合条件则正常插入数据 --开始一个事务处理Begin Tran T1 --执行插入操作insert into Courselist values('201 ...
- HTML基础 整理
HTML:超文本传输协议 (Hyper Markup Language) CSS:网页美化 (Cascading Style Sheets) JS:java-scipt 脚本语言 Dreamweave ...
- js将数组元素随机排序的方法
在群里看见的一个面试题,试了一下,还是可以做出来的,但是需要查资料,主要是岁一些方法了解的不清楚,可能这个跟我平时不太注重基础理论有关系,像什么构造函数啊,我根本就不关心什么叫构造函数,我一直都以为我 ...
- 【转载】使用C#进行系统编程
原文:使用C#进行系统编程 虽然对于系统编程(System programming)的定义很模糊,不过可以将其描述为在比特.字节.指令,或CPU周期层面所进行的思考.系统编程这个概念也暗含了对性能和可 ...
- mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口
搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里