我正在try 转换一个字段的方式从数据库,也在它的方式到数据库.棘手的部分是,我想使用一列中的数据来影响另一列中的数据.
当我硬编码一些所需的信息时,这对我来说是成功的,但我想使用另一个专栏中的信息作为转换过程的一部分.这有可能吗?我想在转换中在相同的上下文中使用数据服务--但它需要行主键,并且可能使它为结果的每条记录执行额外的查询--而且我甚至不能让它工作.任何 idea 都很棒,谢谢你抽出时间来!
// This works:
var converter1 = new ValueConverter<DateTime, DateTime>(
from => MyClass.MyFromMethod(from, "my static text"),
to => MyClass.MyToMethod(to, "my other static text")
);
// This does NOT work: (I want to replace the static text above with somehow getting data from a different column in same row here)
var converter2 = new ValueConverter<DateTime, DateTime>(
from => MyClass.MyFromMethod(from, entity.Property(e => e.OtherColumn).SomehowGetValue()),
to => MyClass.MyToMethod(to, entity.Property(e => e.OtherColumn).SomehowGetValue())
);
// called like this:
modelBuilder.Entity<MyTable>(entity =>
{
entity.ToTable("MyTable");
entity.HasIndex(e => e.MyIdColumn, "MyIndex");
//generated code here..
entity.Property(e => e.MyDateColumn).HasColumnType("datetime")
.HasConversion(converter1); //switching to converter2 ideally!
//more generated code here..