正如标题所述,我可以向我的 POCO 之一添加一个额外的属性,该属性不会映射到数据库列(首先创建数据库),并且不会被持久化。该属性仅在应用程序内使用,不需要持久化。
除了将属性定义为正常属性之外,是否还需要采取任何额外措施来实现此目的?
最佳答案
是的,你绝对可以做到。这是我的配置类的示例:
public class ForCommentEntities:EntityTypeConfiguration<Comment> {
public ForCommentEntities(String schemaName) {
this.HasRequired(e => e.SystemUser)
.WithMany()
.Map(m => m.MapKey("SystemUserID"));
this.Ignore(e => e.Remarks);
this.ToTable("Comment", schemaName);
}
}
this.Ignore
调用是重要的部分。它需要一个 lambda 表达式来表示类的属性之一。这是 EFCF 的伟大之处 (IMO) 的一部分,因为它将配置详细信息保留在 POCO 之外。
配置类将在您的Context
中像这样使用:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
var schemaName = Properties.Settings.Default.SchemaName;
modelBuilder.Configurations
.Add(new Configuration.ForCommentEntities(schemaName))
// ...other configuration options here
;
}
关于c# - 使用 Entity Framework Code First 时,向根本未映射到数据库表的类添加属性是否可以接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7851441/