遮蔽屬性Shadow Properties

陰影屬性是.NET 實體類別中未定義,但該 EF 核心模型中的實體類型定義的屬性。Shadow properties are properties that are not defined in your .NET entity class but are defined for that entity type in the EF Core model. 值,這些屬性的狀態是單純地在變更追蹤程式中維護。The value and state of these properties is maintained purely in the Change Tracker.

不應該公開對應的實體類型的資料庫中的資料時,陰影屬性相當實用。Shadow properties are useful when there is data in the database that should not be exposed on the mapped entity types. 它們通常用於外部索引鍵的屬性,其中兩個實體之間的關聯性由在資料庫中,外部索引鍵值,但使用的實體類型之間的導覽屬性的實體類型上管理關聯性。They are most often used for foreign key properties, where the relationship between two entities is represented by a foreign key value in the database, but the relationship is managed on the entity types using navigation properties between the entity types.

取得與透過變更陰影的屬性值ChangeTracker應用程式開發介面。Shadow property values can be obtained and changed through the ChangeTracker API.

   context.Entry(myBlog).Property("LastUpdated").CurrentValue = DateTime.Now;

您可以透過 LINQ 查詢中參考遮蔽屬性EF.Property靜態方法。Shadow properties can be referenced in LINQ queries via the EF.Property static method.

var blogs = context.Blogs
    .OrderBy(b => EF.Property<DateTime>(b, "LastUpdated"));

慣例Conventions

遮蔽屬性可以在探索到的關聯性,但沒有外部索引鍵屬性相依實體類別中找到時建立依慣例。Shadow properties can be created by convention 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. 陰影的外部索引鍵屬性會被命名為<navigation property name><principal key property name>(相依的實體,指向與主要實體,在瀏覽用來命名)。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). 如果主體索引鍵內容名稱包含導覽屬性的名稱,則只是名稱<principal key property name>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.

例如,下列程式碼清單會導致BlogId陰影屬性所導入Post實體。For example, the following code listing will result in a BlogId shadow property being introduced to the Post entity.

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 應用程式開發介面Fluent API

若要設定陰影的屬性,您可以使用 fluent 應用程式開發的應用程式開發介面。You can use the Fluent API to configure shadow properties. 一旦您所呼叫的字串多載Property您也可以將您對其他屬性設定呼叫鏈結。Once you have called the string overload of Property you can chain any of the configuration calls you would for other properties.

如果名稱提供給Property方法符合名稱的現有屬性 (陰影屬性或其中一個實體類別的定義),則程式碼會設定該現有的屬性,而不是導入新的陰影屬性。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; }
}