陰影屬性Shadow Properties

陰影屬性是.NET 實體類別中未定義,但該 EF Core 模型中的實體類型定義的屬性。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.

取得與透過變更陰影屬性值ChangeTrackerAPI。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 APIFluent API

您可以使用 Fluent API,若要設定陰影屬性。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; }
}