-
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
Use C# structs or classes as value objects #9906
Comments
Good point. |
It seems like nullable owned entities are also not supported. |
@sir-boformer - nullable owned entities are covered by #9005 If you map them to different table then they can work. |
Why would you want to map an owned entity to a different table ? In most cases, I want them to be part of the owning table. Nullable owned entities are a valid use-case, especially if the database already exists ... |
@fgheysels - That is valid use-case. At present, when they are mapped in same table, they uses same column for PK. In current implementation there is no way to figure out if the owned entity is null. So if you map it to separate table, you can find out owned entity is null when no matching row is found. It is not ideal and users would want to map it to same table hence #9005 is the tracking issue to implement support for it. |
Notes from triage: we think the common cases for structs like these could be handled by being able to map a type to multiple columns. The main missing feature for this is type mapping from a single property to multiple columns. |
@smitpatel - I think that , when using owned entities as a value type (which will be for me the most obvious usage of owned entities), you should not think in terms of primary keys. A value type is not an entity and has no identifier; it's identity is made up by its value. @ajcvickers I'm a fond user of NHibernate, and there this is done using 'component-mapping'. Maybe helpfull to take a look at it ? http://nhibernate.info/doc/nhibernate-reference/components.html |
Note: consider the case that the value object contains key properties--see #10682 |
This prevents me from reusing my models, as I'm using struct for related properties set heavily. |
It works for classes but not for structs. If you try to use It says clearly that |
See also #13947 (comment) |
Would this allow support for ValueObjects implemented with C# Records? Using classes when we could leverage records to implement a ValueObject on our model is kind of cumbersome. |
@CesarD probably, yes. |
Two questions:
|
Probably yes, although performing SaveChanges updates on immutable types (whether value types or otherwise) is tracked by #11457.
What do you mean by that? |
Base class with derived types for value objects, but i realise now that this can be done with json right?. |
It's hard to know at this point exactly what will be supported and what won't - there's no specific plan yet for this feature. |
🎉 |
Now that Currently it's only allowed on [AttributeUsage(Class /*| Struct*/, AllowMultiple=false)]
public class ComplexTypeAttribute : Attribute { ... } |
@NickStrupat The team decided not to do this. /cc @AndriySvyryd |
@NickStrupat You can also call |
Nor micrososft provides any example. Here is what I have tried: in the ConfigureConventions I have defined the complex property:
In the entity I define the property: This gives an exception: No suitable constructor was found for entity type 'Ebtitiy.MyStruct?'. The following constructors had parameters that could not be bound to properties of the entity type: |
I was really looking forward to the new Owned Entities feature until I found out that struct types are not supported.
Common examples of struct types:
Similar Struct Types in third party libraries: corefxlab NodaTime
I would like to use these struct types in my entities:
so that the generated tables look like these:
The text was updated successfully, but these errors were encountered: