I am trying to convert a field on way in from the database, and also on its way out to the database. The tricky part is, I would like to use data from one column to affect data in a different column.
This has worked for me successfully when I hard-code some of the information needed, but I would like to use information from another column as part of the conversion process. Is that possible? I thought of using a dataservice inside the conversion, on the same context - but it would need the rows primary key, and might make it do an extra query for every record the result -- plus I couldn't even get that to work. Any ideas would be great, thanks for your time!
// 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..