EFCore 2 Fluent API 构建自定义映射规则
EFCore 2 Fluent API to build custom mapping rule
我遇到这样一种情况,我正在尝试构建一个与数据库不完全匹配的域模型,并且我在尝试弄清楚如何使用 EFCore[=14] 构建映射规则时遇到了困难=]
约会域模型
public class Appointment
{
public string Title { get; set; }
public long UserId { get; set; }
public string UserFullName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public void MyBusinessLogic(){}
}
这是精简版,但要点是我不想在我的约会模型中将完整的用户对象作为 属性,例如:
public User User {get;set;} <== trying to avoid adding this to the class
我只想要 UserId 和一些次要的元数据,例如用户的全名。
我已经为 EFCore 映射设置了一个配置,以仍然构建和映射 FK 关系,如下所示
internal class AppointmentConfiguration : IEntityTypeConfiguration<Appointment>
{
public void Configure(EntityTypeBuilder<Appointment> builder)
{
builder.Property(x => x.Title)
.IsRequired(true)
.HasMaxLength(AppointmentConst.MaxTitleLength);
builder.Property(x => x.Description)
.IsRequired(true)
.HasMaxLength(AppointmentConst.MaxDescriptionLength);
builder.HasOne<User>()
.WithMany()
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Restrict);
}
}
所以我想弄清楚的是,是否可以通过连接第一个和最后一个来为 UserFullName 属性 构建某种映射规则以从用户 table 读取数据命名列,但永远不要写入 table.
几乎就像使用 Automapper 这样的工具所做的那样。
基本上,我只想对 EFCore 说,当我查询约会数据时,从用户 table 获取 UserFullName 的值,并将 FirstName 和 LastName 列的值连接起来
您不能直接映射相关实体的属性,但您可以通过使用私有 property/field.
来解决污染模型的问题
public class Appointment
{
...
private User user;
public string UserFullName => $"{user.FirstName} {user.LastName}";
...
}
builder.HasOne<User>("user")
.WithMany()
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Restrict);
在构建查询时不要忘记 .Include("user")
。
另请注意,您的 DbContext 现在正在跟踪此用户,如果您修改它,它的更改将在稍后 SaveChanges
调用时保存到数据库中。
我遇到这样一种情况,我正在尝试构建一个与数据库不完全匹配的域模型,并且我在尝试弄清楚如何使用 EFCore[=14] 构建映射规则时遇到了困难=]
约会域模型
public class Appointment
{
public string Title { get; set; }
public long UserId { get; set; }
public string UserFullName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public void MyBusinessLogic(){}
}
这是精简版,但要点是我不想在我的约会模型中将完整的用户对象作为 属性,例如:
public User User {get;set;} <== trying to avoid adding this to the class
我只想要 UserId 和一些次要的元数据,例如用户的全名。
我已经为 EFCore 映射设置了一个配置,以仍然构建和映射 FK 关系,如下所示
internal class AppointmentConfiguration : IEntityTypeConfiguration<Appointment>
{
public void Configure(EntityTypeBuilder<Appointment> builder)
{
builder.Property(x => x.Title)
.IsRequired(true)
.HasMaxLength(AppointmentConst.MaxTitleLength);
builder.Property(x => x.Description)
.IsRequired(true)
.HasMaxLength(AppointmentConst.MaxDescriptionLength);
builder.HasOne<User>()
.WithMany()
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Restrict);
}
}
所以我想弄清楚的是,是否可以通过连接第一个和最后一个来为 UserFullName 属性 构建某种映射规则以从用户 table 读取数据命名列,但永远不要写入 table.
几乎就像使用 Automapper 这样的工具所做的那样。
基本上,我只想对 EFCore 说,当我查询约会数据时,从用户 table 获取 UserFullName 的值,并将 FirstName 和 LastName 列的值连接起来
您不能直接映射相关实体的属性,但您可以通过使用私有 property/field.
来解决污染模型的问题public class Appointment
{
...
private User user;
public string UserFullName => $"{user.FirstName} {user.LastName}";
...
}
builder.HasOne<User>("user")
.WithMany()
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Restrict);
在构建查询时不要忘记 .Include("user")
。
另请注意,您的 DbContext 现在正在跟踪此用户,如果您修改它,它的更改将在稍后 SaveChanges
调用时保存到数据库中。