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:

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 » 影子属性的更多相关文章

  1. 【ASP.NET Core】EF Core - “影子属性” 深入浅出经典面试题:从浏览器中输入URL到页面加载发生了什么 - Part 1

    [ASP.NET Core]EF Core - “影子属性”   有朋友说老周近来博客更新较慢,确实有些慢,因为有些 bug 要研究,另外就是老周把部分内容转到直播上面,所以写博客的内容减少了一点. ...

  2. 【ASP.NET Core】EF Core - “影子属性”

    有朋友说老周近来博客更新较慢,确实有些慢,因为有些 bug 要研究,另外就是老周把部分内容转到直播上面,所以写博客的内容减少了一点. 老周觉得,视频直播可能会好一些,虽然我的水平一般,不过直播时,老周 ...

  3. C# 数据操作系列 - 7. EF Core 导航属性配置

    在上一篇,大概介绍了Entity Framework Core关于关系映射的逻辑.在上一篇中留下了EF的外键映射没有说,也就是一对一,一对多,多对一,多对多的关系等.这一篇将为大家细细分析一下,如何设 ...

  4. 【ASP.NET Core】EF Core - “导航属性”

    “导航属性”是实体框架用得算是比较频繁的概念. 首先,它是类型成员,其次,他是属性,这不是 F 话,而是明确它的本质.那么,什么场景下会用到导航属性呢?重点就落在“导航”一词上了,当实体 A 需要引用 ...

  5. C# 数据操作系列 - 8. EF Core的增删改查

    0.前言 到目前为止,我们看了一下如何声明EF Core的初步使用,也整体的看了下EF Core的映射关系配置以及导航属性的配置. 这一篇,我带大家分享一下,我在工作中需要的EF Core的用法. 1 ...

  6. EF Core中Key属性相同的实体只能被跟踪(track)一次

    在EF Core的DbContext中,我们可以通过DbContext或DbSet的Attach方法,来让DbContext上下文来跟踪(track)一个实体对象,假设现在我们有User实体对象,其U ...

  7. EF Core中怎么实现自动更新实体的属性值到数据库

    我们在开发系统的时候,经常会遇到这种需求数据库表中的行被更新时需要自动更新某些列. 数据库 比如下面的Person表有一列UpdateTime,这列数据要求在行被更新后自动更新为系统的当前时间. Pe ...

  8. EF Core中如何通过实体集合属性删除从表的数据

    假设在数据库中有两个表:Person表和Book表,Person和Book是一对多关系 Person表数据: Book表数据: 可以看到数据库Book表中所有的数据都属于Person表中"F ...

  9. EF Core反向导航属性解决多对一关系

    多对一是一种很常见的关系,例如:一个班级有一个学生集合属性,同时,班级有班长.语文课代表.数学课代表等单个学生属性,如果定义2个实体类,班级SchoolClass和学生Student,那么,班级Sch ...

随机推荐

  1. Scala循环操作

    val buf = new scala.collection.mutable.ArrayBuffer[Int] for (i <- 0 to 10) { buf += i } buf += 11 ...

  2. zookeeper系列之十一—zookeeper会话超时

    1.会话概述 在Zookeeper中,客户端和服务端建立连接后,会话随之建立,生成一个全局唯一的会话ID(Session ID).服务器和客户端之间维持的是一个长连接,在SEESSION_TIMEOU ...

  3. ContentProvider官方教程(8)自定义MIME

    MIME Type Reference Content providers can return standard MIME media types, or custom MIME type stri ...

  4. UVA 10341 Solve It 二分

    题目大意:给6个系数,问是否存在X使得等式成立 思路:二分.... #include <stdio.h> #include <math.h> #define EEE 2.718 ...

  5. Utility

    #region Tools public static string ForMatDateTime(DateTime dt, string formatStr) { return string.For ...

  6. Cheatsheet: 2014 01.15 ~ 01.30

    Web How to upload file in Node.js Create Echo Server in Node.js Near-Realtime Analytics with MongoDB ...

  7. Linux运行变量中的命名脚本

    single="ls -l" $single ============= multi="ls -l | grep e" echo $multi > tmp ...

  8. django-crontab定时任务

    django-crontab实现定时任务 1 django-crontab安装 django-crontab安装: django-crontab加入:只需要将INSTALLED_APPS即可.如下代码 ...

  9. PE结构笔记

    提示:前面加*为必须背下来的 DOS头: typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; //* Magic n ...

  10. poj 2653 (线段相交判断)

    http://poj.org/problem?id=2653 Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submis ...