none
Complex Type with Complex Property Collection RRS feed

  • Question

  • In EF 6, is there any way to add a property to a Complex Type that is a List of another Complex Type?  For simplicity, I am just making up a rather unrealistic example:  I have a Complex Type called Person and another Complex Type called Order.  A Person could have a property that is a List of Order items.  Obviously, EF is set up to handle this if these were Entities.  However, in my case, I am passing in the Person properties to a set Function Import and passing in the Order properties to another Function Import.  I really do not want to expose the Person or Order tables in my database to the outside as only Insert is allowed.  Also, a Person cannot be added unless they have at least one Order.  I know I can map the Insert of an Entity to a Function Import, but I am not sure how to handle preventing the ability to Update, Delete, or Select on these entities.  I had thought using Complex Types would work, but then I have the problem of needing a list of a Complex Property. 

    Thanks in advance for your help!


    Christine A. Piffat

    Thursday, October 24, 2013 5:31 PM

Answers

  • public partial class Person
    {
        public string Name { get; set; }
        ...
        public List<Order> Orders { get; set; }
    }
    
    public partial class Order
    {
        public DateTime OrderDate { get; set; }
        ...
    }

    This is a very normal relationship that shows Person:Order is 1:N.

    Of course it is possible, please see it:

    http://mikebanner.blogspot.in/2011/10/entity-framework-41-creating-one-to.html

    If I have misunderstood, please let me know


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 29, 2013 10:29 AM
    Moderator
  • Not quite what I needed.  My understanding of this post was that it was still using Entities, not Complex Types.  In a somewhat simpler situation (where I was going the other direction--stored proc gives complex type with list of complex types), I was told that it wouldn't work on stackoverflow

    I ended up just solving my current problem by creating two Complex types (Person and Order).  I then created a new class (PersonData) that inherited from Person and added a List<Order> property.  Finally, I created a method that took a PersonData parameter and called both of my insert FunctionImports within a TransactionScope, passing in the appropriate fields from the PersonData object.   I think that will work for what I need.


    Christine A. Piffat

    • Marked as answer by Christy Piffat Thursday, October 31, 2013 9:09 PM
    Thursday, October 31, 2013 9:09 PM

All replies

  • It seems that there is no ways to do this as you want even in Entity Framework 6.

    New features in Entity Framework:

    http://entityframework.codeplex.com/wikipage?title=specs

    Friday, October 25, 2013 8:58 AM
  • I was afraid of that.  I guess if I don't map any function imports to the update or delete on these entities this might work.  It will just allow a select I would rather not include. 

    On a related note, does anyone know if EF 6 allows for better handling of multiple result sets than this article about EF 5? 

    Thanks!


    Christine A. Piffat

    Friday, October 25, 2013 9:52 PM
  • Hello,

    If I understand you correctly, you want to let the complex types be nested inside classes.

    For this, fortunately, Entity Framework 6 can support this, we can see it in Entity Framework 6 features:

    http://entityframework.codeplex.com/wikipage?title=specs

    It says that

    • >>Entity and complex types can now be nested inside classes.

     >>does anyone know if EF 6 allows for better handling of multiple result sets than this article about EF 5? 

    It seems that there is no updatas for this in Entity framework 6.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 28, 2013 11:03 AM
    Moderator
  • Yes, I would need something in my POCO classes such as

    public partial class Person
    {
        public string Name { get; set; }
        ...
        public List<Order> Orders { get; set; }
    }
    
    public partial class Order
    {
        public DateTime OrderDate { get; set; }
        ...
    }

    Is this possible?  I looked at CodePlex, but I can't find an example of how to do this.  Also, I have tried setting up the complex types manually, but it only allows a single one of the complex type within a complex type. 

    I am using Database First to do this since the database is already set up.

    Thanks!


    Christine A. Piffat

    Monday, October 28, 2013 8:14 PM
  • public partial class Person
    {
        public string Name { get; set; }
        ...
        public List<Order> Orders { get; set; }
    }
    
    public partial class Order
    {
        public DateTime OrderDate { get; set; }
        ...
    }

    This is a very normal relationship that shows Person:Order is 1:N.

    Of course it is possible, please see it:

    http://mikebanner.blogspot.in/2011/10/entity-framework-41-creating-one-to.html

    If I have misunderstood, please let me know


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 29, 2013 10:29 AM
    Moderator
  • Not quite what I needed.  My understanding of this post was that it was still using Entities, not Complex Types.  In a somewhat simpler situation (where I was going the other direction--stored proc gives complex type with list of complex types), I was told that it wouldn't work on stackoverflow

    I ended up just solving my current problem by creating two Complex types (Person and Order).  I then created a new class (PersonData) that inherited from Person and added a List<Order> property.  Finally, I created a method that took a PersonData parameter and called both of my insert FunctionImports within a TransactionScope, passing in the appropriate fields from the PersonData object.   I think that will work for what I need.


    Christine A. Piffat

    • Marked as answer by Christy Piffat Thursday, October 31, 2013 9:09 PM
    Thursday, October 31, 2013 9:09 PM