-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement complex types (value objects) #13947
Comments
@divega to look for dupe. |
This issue could be considered a duplicate of #9906 "Use C# structs or classes as value objects", based on the triage notes at #9906 (comment), and the fact that #13067 was previously closed in favor of #9906. But I personally don't think the association is clear, especially given that we don't have a design for #9906. I would prefer to keep this issue open to represent the desire to have value conversions that spread over multiple columns, so that customers can vote for it and discuss it independently of structs and value objects. |
Having value conversions spread over multiple columns is exactly what I was hoping I could do. I have a property type (Length) in my data model which is defined as a struct in a third party library. It contains two critical data fields - the value (double) and the unit (enum). I simply want to map the value to a double column and the string-converted-enum to another column. As a workaround, I ended up going with the same approach as the original poster. It is undesirable since it muddies up the data model: Entity configuration in DbContext via Fluent API: EntityTypeBuilder<PhysicalBraid> physicalBraidBuilder = modelBuilder.Entity<PhysicalBraid>();
physicalBraidBuilder.Property(p => p.Notes).HasColumnType("xml").HasConversion(v => XamlWriter.Save(v), v => XamlReader.Parse(v) as FlowDocument);
physicalBraidBuilder.Property<double>("measuredLength_Value");
physicalBraidBuilder.Property<LengthUnit>("measuredLength_Unit").HasConversion(new EnumToStringConverter<LengthUnit>()); Entity definition in Data Model: public class PhysicalBraid : INotifyPropertyChanged {
public int Id { get; set; }
[Required]
public virtual BraidDesign Design { get; set; }
[Required]
public virtual TurntableConfiguration StartConfiguration { get; set; }
public virtual ObservableCollection<PhysicalBraidState> States { get; set; } = new ObservableCollection<PhysicalBraidState>();
[NotMapped]
public Length MeasuredLength {
get => new Length(measuredLength_Value, measuredLength_Unit);
set { measuredLength_Value = value.Value; measuredLength_Unit = value.Unit; }
}
private double measuredLength_Value { get; set; }
private LengthUnit measuredLength_Unit { get; set; }
public FlowDocument Notes { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
} |
+1 please... |
An additional note: For me it would be very interesting to map a combined key (2 columns) with a conversion in a single class or struct. That also means, that I would need to query this combined key, and the restriction should be evaluated in the database and not on the client. Otherwise it would load way to much data. An example is in the issue #15962. |
This would enable really painless audit trails for EF-Core, as what is currently standing in the way is that there are issues in getting the changed values of Value Objects. Related SO: https://stackoverflow.com/questions/60946496/ef-core-how-to-audit-trail-with-value-objects |
Add model validation rules for complex types Discover public fields on complex types Add support for ComplexTypeAttribute Part of #13947
So, is this implemented now? |
@DumboJet Docs are behind. See Value objects using Complex Types. |
Greetings, this is a question/feature request:
Per the Limitations section of the Value Conversions documentation: "There is currently no way to spread a conversion of one property to multiple columns or vice-versa."
Is something like this planned, or is there an alternative better way to do this?
I'm looking for a way to keep the backing fields out of my entity classes. And to not use strings in the configuration or public backing fields in my entity classes.
Here's a quick example from my current code working with a legacy DB:
From an entity class:
From the entity's configuration:
Thanks for your time!
The text was updated successfully, but these errors were encountered: