英文:
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<Student>().Property<DateTime>("CreatedDate");
modelBuilder.Entity<Student>().Property<DateTime>("UpdatedDate");
}
public DbSet<Student> 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("CreatedDate")
.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
).
答案2
得分: 0
"实体中的"影子属性"概念在对象关系映射(ORM)框架如Entity Framework中使用,用于向实体添加附加属性,而不需要修改数据库模式。换句话说,这些属性不会映射到数据库表中的任何列,但可以用于存储不需要持久化的信息。
在您提供的第一个示例中,有两个影子属性:
"CreatedDate"
和 "UpdatedDate"
它们通过以下方式添加到"Student"
实体中:
"modelBuilder.Entity<Student>().Property<DateTime>("PropertyName")"
这些属性可用于跟踪学生记录的创建和最后更新时间,而无需将这些列添加到底层数据库表中。
在第二个示例中,"BaseEntity"
类用于定义多个实体共享的公共属性。在这种情况下,"CreatedDate"
和 "UpdatedDate"
属性在基类中定义,并且"Student"
类从"BaseEntity"
类继承。这种方法消除了为每个实体类定义这些属性的需要,并确保它们在所有实体中得到一致实现。
希望这有所帮助
英文:
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:
"CreatedDate"
and "UpdatedDate"
They are added to the "Student" entity using the
"modelBuilder.Entity<Student>().Property<DateTime>("PropertyName")"
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 "BaseEntity"
class is used to define common properties that are shared by multiple entities. In this case, the "CreatedDate"
and "UpdatedDate"
properties are defined in the base class, and the "Student"
class inherits from the "BaseEntity"
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论