在实体中定义“影子属性”的目的是什么?

huangapple go评论67阅读模式
英文:

What is the point of defining "shadow properties" in Entities?

问题

这个问题可能看起来有点傻,但在实体中定义“影子属性”的意义是什么呢,因为相应的列会在数据库/表中创建?此外,我们可以通过继承来解决这个问题,所以假设 EF 没有引入“影子属性”,那会怎样?

我可以通过其他方式实现什么,而不是使用我手头已有的东西?

让我们看一些代码:

public class Student
{
   public int StudentID { get; set; }
   public string StudentName { get; set; }
   public DateTime? DateOfBirth { get; set; }
   public decimal Height { get; set; }
   public float Weight { get; set; }
}

public class SchoolContext : DbContext
{
   public SchoolContext() : base()
   {		
   }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      modelBuilder.Entity<Student>().Property<DateTime>("CreatedDate");
      modelBuilder.Entity<Student>().Property<DateTime>("UpdatedDate");
   }

   public DbSet<Student> Students { get; set; }
}

对比:

public class BaseEntity
{
   public DateTime CreatedDate { get; set; }
   public DateTime UpdatedDate { get; set; }
}

public class Student : BaseEntity
{
   public int StudentID { get; set; }
   public string StudentName { get; set; }
   public DateTime? DateOfBirth { get; set; }
   public decimal Height { get; set; }
   public float Weight { get; set; }
}

这难道不会使实现变得更加复杂吗?

英文:

This question may seem silly,
but what is the point of defining "shadow properties" in Entities since the corresponding column would be created in Database/Tables?
Plus, we can simply solve this issue by inheritance so imagine "shadows properties" has NOT been introduced by EF, then what?

What could not I implement by other things that I have had in my hands?

let's look at some code:

public class Student
{
   public int StudentID { get; set; }
   public string StudentName { get; set; }
   public DateTime? DateOfBirth { get; set; }
   public decimal Height { get; set; }
   public float Weight { get; set; }
}

public class SchoolContext : DbContext
{
   public SchoolContext() : base()
   {		
   }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
	  modelBuilder.Entity&lt;Student&gt;().Property&lt;DateTime&gt;(&quot;CreatedDate&quot;);
	  modelBuilder.Entity&lt;Student&gt;().Property&lt;DateTime&gt;(&quot;UpdatedDate&quot;);
   }

   public DbSet&lt;Student&gt; Students { get; set; }
}

VS

public class BaseEntity
{
   public DateTime CreatedDate
   public DateTime UpdatedDate
}

public class Student : BaseEntity
{
   public int StudentID { get; set; }
   public string StudentName { get; set; }
   public DateTime? DateOfBirth { get; set; }
   public decimal Height { get; set; }
   public float Weight { get; set; }
}

doesn't it make the implementation more complicated?

答案1

得分: 1

以下是翻译好的部分:

关键是阴影属性不存在于实体类上,而是在实体模型中定义的。您不会通过类实例访问这些类型的属性。您会使用 DbContext 模型管理器(与创建阴影属性的方式相同)。

private DateTime GetCreatedDate(Student student)
{
  using dbContext = new MyDbContext();
  var createdDate = (DateTime)dbContext
    .Entry(student)
    .Property("CreatedDate")
    .CurrentValue; 
}

由您决定是否要在实体类中包含与模型相关的元属性(例如关系属性,如外键 ID 和其他元数据)。如果不需要,您可以在模型中定义阴影属性。

在这种情况下,元数据指的是关于实际数据记录(表行)的数据(表列)。这可以是时间戳、读/写计数器或数据关系相关的数据,如外键。通常,应用程序不关心这种元信息。您不希望用元数据污染您的数据实体,因此您会将它们定义为仅存在于实体模型中(由 DbContext 管理的)的阴影属性。

阴影属性和索引器属性

英文:

The key is that shadow properties don't exist on the entity class. Instead they are defined in the entity model. You don't access those kind of properties via a class instance. You use the DbContext model manager (same way as shadow properties are created).

private DateTime GetCreatedDate(Student student)
{
  using dbContext = new MyDbContext();
  var createdDate = (DateTime)dbContext
    .Entry(student)
    .Property(&quot;CreatedDate&quot;)
    .CurrentValue; 
}

It's up to you to decide whether you want model related meta properties (for example relation properties like foreign key IDs and other meta data) in your entity class. If you don't, you can define shadow properties in the model.
Meta data in this case means data (table columns) about the actual data records (table rows). This could be a timestamp, a read/write counter or data relation related data like foreign keys. Usually the application is not interested in such meta information. You don't want to pollute your data entity with meta data. Hence you would define them as shadow properties, that only exist in the entity model (managed by DbContext).

Shadow and Indexer Properties

答案2

得分: 0

"实体中的"影子属性"概念在对象关系映射(ORM)框架如Entity Framework中使用,用于向实体添加附加属性,而不需要修改数据库模式。换句话说,这些属性不会映射到数据库表中的任何列,但可以用于存储不需要持久化的信息。

在您提供的第一个示例中,有两个影子属性:

&quot;CreatedDate&quot;&quot;UpdatedDate&quot;

它们通过以下方式添加到&quot;Student&quot;实体中:
&quot;modelBuilder.Entity&lt;Student&gt;().Property&lt;DateTime&gt;(&quot;PropertyName&quot;)&quot;
这些属性可用于跟踪学生记录的创建和最后更新时间,而无需将这些列添加到底层数据库表中。

在第二个示例中,&quot;BaseEntity&quot; 类用于定义多个实体共享的公共属性。在这种情况下,&quot;CreatedDate&quot;&quot;UpdatedDate&quot; 属性在基类中定义,并且&quot;Student&quot; 类从&quot;BaseEntity&quot; 类继承。这种方法消除了为每个实体类定义这些属性的需要,并确保它们在所有实体中得到一致实现。

希望这有所帮助 在实体中定义“影子属性”的目的是什么?

英文:

The concept of "shadow properties" in entities is used in Object-Relational Mapping (ORM) frameworks like Entity Framework to add additional properties to an entity without modifying the database schema. In other words, these properties are not mapped to any column in the database table, but can be used to store information that is not persisted.

In the first example you provided, the two shadow properties:

&quot;CreatedDate&quot; and &quot;UpdatedDate&quot;

They are added to the "Student" entity using the
&quot;modelBuilder.Entity&lt;Student&gt;().Property&lt;DateTime&gt;(&quot;PropertyName&quot;)&quot;
method. These properties can be used to track when a student record was created and last updated, without having to add these columns to the underlying database table.

In the second example, the &quot;BaseEntity&quot; class is used to define common properties that are shared by multiple entities. In this case, the &quot;CreatedDate&quot; and &quot;UpdatedDate&quot; properties are defined in the base class, and the &quot;Student&quot; class inherits from the &quot;BaseEntity&quot; class. This approach eliminates the need to define these properties for every entity class and ensures that they are consistently implemented across all entities.

I hope this helps 在实体中定义“影子属性”的目的是什么?

huangapple
  • 本文由 发表于 2023年5月7日 18:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定