Complex types and/or value objects
This issue is tracking the inclusion of support for either complex types and/or value objects in EF7.x (we haven't decided what form exactly this will take or when we will do it but it is not planned for EF7.0.0 RTM).
Complex Type just means a class with no primary key - i.e. it's just a way of organizing some properties that are part of the object it is referenced from. This has been called complex types traditionally in EF.
Value objects also can be seen as a set of properties grouped under the same type that don't have a key, although there is a more formal definition and characteristics, e.g. they should be immutable, equality should be defined the values of their individual properties rather than on a key, copy value semantics is desirable.
When doing this we should consider different strategies for how the data is stored. In EF6 we had a column in the parent table for each property in the complex type. Another strategy that folks may want to chose if serializing the data as JSON/XML etc. (see https://github.com/aspnet/EntityFramework/issues/3486)
Is there a plan for when this will be implemented?
@npehrsson no exact timeline at the moment. It's high on the backlog, close behind TPH inheritance support and navigation property translation in LINQ (which are both being implemented at the moment).
Will this make it into Beta 7? Beta 8? RC? If not by RC I would assume it isn't making it by RTM.
This feature has been lacking since the beginning of EF and yet has been in competitive alternatives. Without this feature EF7 will still be at a competitive disadvantage. This is a huge feature to allow people to create data models naturally for their domains without having to shoe horn them into compatible types supported by EF.
@CreepyGnome correct, this one is triaged for post initial stable release. This would definitely fall into the bucket of features that means EF6.x will be the right choice for many apps for a while.
First what do you guys mean by Complex Types here?
What I mean are the ones within the .NET Framework like IDictionary, ILookup, Money, etc. I wasn't worried EF7 supporting my POCO Entities and nesting Entities within other Entities. However now I am concerned.
Technically all Domain Models are nothing but a hierarchy of Custom Complex Types that we typically like to call POCOs. I would have to say supporting custom complex types like these are mandatory for any ORM otherwise what are you mapping really? However your answer makes me think EF7 will be like EF5 and lower when it comes to any Complex Type and Nesting of them. If so that is a huge step backwards.
So are you saying EF7 doesn't support ANY complex types what so ever bet that .NET Framework or custom ones you create? If so that means EF7 doesn't supporting nesting of entities.
Also am I to assume that if it is flagged as being in the backlog, them it means no release is targeted at this time period.
I thought this was value objects, value objects = complex type in ef6, not sure why they didn't call it value object mapping. And I agree not supporting this is a pain for us that want to use the beta. On Aug 25, 2015 12:36 AM, "Rodney S. Foley" [email protected] wrote:
First what do you guys mean by Complex Types here?
What I mean are the ones within the .NET Framework like IDictionary, ILookup, Money, etc. I wasn't worried EF7 supporting my POCO Entities and nesting Entities within other Entities. However now I am concerned.
Technically all Domain Models are nothing but a hierarchy of Custom Complex Types that we typically like to call POCOs. I would have to say supporting custom complex types like these are mandatory for any ORM otherwise what are you mapping really? However your answer makes me think EF7 will be like EF5 and lower when it comes to any Complex Type and Nesting of them. If so that is a huge step backwards.
So are you saying EF7 doesn't support ANY complex types what so ever bet that .NET Framework or custom ones you create? If so that means EF7 doesn't supporting nesting of entities.
Also am I to assume that if it is flagged as being in the backlog, them it means no release is targeted at this time period.
— Reply to this email directly or view it on GitHub https://github.com/aspnet/EntityFramework/issues/246#issuecomment-134400942 .
Hey,
Complex Type just means a class with no primary ke - i.e. it's just a way of organizing some properties that are part of the object it is referenced from. They have historically been called "Complex Types" in EF (before my time) but I think most folks would expect a value type to be immutable (which complex types are not - though you can make the property setters private to somewhat achieve this).
Correct that this will not be in the November RC. We haven't allocated any work items to release beyond that. This is listed on our roadmap as a critical O/RM feature and something we need implemented before we start saying that EF7 is a serious alternative to EF6 for all apps.
~Rowan
What you described as a complex type is broad and to support that would be difficult, unless supported the way EF6 claims to support a much more limited version of Complex Types definition.
I mean a Dictionary and other core classes, don't have ID's and yet are complex types but was never supported before in EF, but is supported by others ORMs. If EF7 is only trying to be at the same level as EF6 with this feature request here, is there another feature in the backlog for support things like Dictionary<TKey, TElement>, Lookup<TKey, TElement>, HashSet, (all collections basically, Money, etc from the core .NET Framework?
@npehrsson Well the title is "Complex and/or value types" so they are show a clear distinction between the two. Value Type here I would assume means structs (since they already support primitives), and Complex Types would mean every other type of object that doesn't have a primary key (aka entity). However it seems that Complex Type here may mean something more limited in this feature. Since the feature has no description to clarify what its intent is, it will always be vague in what they plan to implement vs what the true definition of those words in C#.
Value Type here I would assume means structs
@CreepyGnome We didn't mean structs although they could be structs. I think the correct title would be "Complex Types and/or values objects". I can fix that. We punted on the decision on whether we would just support "complex types" in the traditional EF sense or purer DDD-style value objects.
Since the feature has no description to clarify what its intent is
Will fix this as well. @rowanmiller feel free to make edits.
is there another feature in the backlog for support things like Dictionary, Lookup, HashSet, (all collections basically), Money, etc from the core .NET Framework?
I don't think we currently have an issue for rich collection support (I thought we did have something in the backlog but I couldn't find it). It should include mapping to ordered collections as well.
I see this as a separate feature from rich scalar property support. For the latter, providers can support different scalar types if they can handle them natively and we have an item to extend that using conversions at https://github.com/aspnet/EntityFramework/issues/242.
@CreepyGnome I have created https://github.com/aspnet/EntityFramework/issues/2919 for rich collection support.
Just to add some PostgreSQL perspective here... PG supports user-defined composite types: users can define new database types with arbitrary fields, and then use those types as columns in tables. Npgsql 3.1 (the current dev version) includes first-class support for these at the ADO.NET level by allowing the user to map a CLR type to composite types. It would probably be trivial for the Npgsql EF7 TypeMapper to expose these mappings to EF7, allowing arbitrary objects to be mapped to columns. This would be very similar to mapping an object to a JSON/XML field, except it's much more efficient.
I don't think all this has any bearing on EF7 (totally PostgreSQL- and Npgsql-specific) but it's related.
@roji this is great info to have, thanks!
Please, consider to add navigation property to the complex type. For example money is value object (domain driven design). A money has got an amount and a currency, but we are not interested in the id of the money, we just would like to represent 112.5 USD and store that in the database. But the currency is an entity in our model with a primary key. So we should be able to navigate from our money object to the currency.
It should be something like this: [ComplexType] public class Money { public decimal Amount { get; set; } public Currency Currency { get; set; } }
Thanks!
Are complex types supported yet? In EF6 you could declare a property as an object, without a Key and it would create the columns in the same table. For example,
public class MainObject() {
public long Id { get; set; }
public SubProperty Sub { get; set; }
}
// No key defined here as shouldnt need it
public class SubProperty() {
public string Text { get; set; }
}
I have tried this in rc1 and i am getting an error the following, but maybe i am not setting something up correctly.
The entity type 'SubProperty' requires a key to be defined.
@Gillardo no, this item is on the backlog and will be tackled post 7.0.0
I have a db context, and I want to add support for tracking the user that added, modified and virtually deleted the record. To to this I added to the class the required properties
public interface ILoggedTable
{
int UtenteCreazioneId { get; set; }
DateTime CreazioneTs{ get; set; }
Utente UtenteCreazione { get; set; }
int ?UtenteModificaId { get; set; }
DateTime ?ModificaTs{ get; set; }
Utente UtenteModifica { get; set; }
int? UtenteCancellazioneId { get; set; }
DateTime ?CancellazioneTs{ get; set; }
Utente UtenteCancellazione { get; set; }
}
and this implies that in the user entity I'll have 3 collections of entity has been deleted,added, modfied by the user, for every table. To avoid this I would like to group these navigation properties in a generic class this way:
public class LoggedTableRevProps<T> where T : class
{
public virtual ICollection<T> Creati { get; set; }
public virtual ICollection<T> Modificati { get; set; }
public virtual ICollection<T> Cancellati { get; set; }
public LoggedTableRevProps() {
Creati = new HashSet<T>();
Modificati = new HashSet<T>();
Cancellati = new HashSet<T>();
}
}
All seems work fine, but when I try to create the migration I obtain this error message
The expression 'p => p.UtentiLogProps.Creati' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'.
Nome parametro: propertyAccessExpression
Is this the same problem?
@lucamorelli - It is different issue. Can you file a new issue with your model classes & your DbContext code?
@rowanmiller Complex type is the name of keyless object in OData too.
Complex types are keyless named structured types consisting of a set of properties. These are value types whose instances cannot be referenced outside of their containing entity. Complex types are commonly used as property values in an entity or as parameters to operations.
@smitpatel I created this issue https://github.com/aspnet/EntityFramework/issues/4564 with the link to the sources of the sampe