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:

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; }
    }
 
原文地址:https://www.cnblogs.com/jqdy/p/5964665.html