none
EF5 & VS11 Beta - Bug? - NotSupportedException: Only parameterless constructors... using Linq to XML to generate XElements and XAttributes RRS feed

  • Question

  • I am getting the exception:

    'System.NotSupportedException: Only parameterless constructors and initializers are supported in LINQ to Entities.'

    When trying to generate XElemet(s) and XAttribute(s) from an EF5 model.

    The code that is generaqting the exception is:

    XElement root = new XElement("MusicLibrary",
        new XAttribute("DateTime",
            DateTime.Now.ToShortDateString( ) + " " +
            DateTime.Now.ToShortTimeString( )));
    var elements = from artist in cont.Artists1
                   let ArtistName = artist.ArtistInfo.Title
                   let ArtistAlbumns = artist.Albums.Select(albums =>
                        new XElement("Album",
                            new XAttribute("Album", albums.AlbumInfo.Title)))
                   select new XElement("Artist",
                       new XAttribute("Name", ArtistName),
                            ArtistAlbumns);
    root.Add(elements); // <- Exception thrown here

    Where:

    • ArtistInfo and AlbumInfo are an EF Complex Type, and
    • Artist to Album is a one (Artist) to Many (Albums) relationship.

    I would appera thet that EF is trying to interpret the new XElement and new XAttribute calls as attempts to make "data" which fits into the EF model. If needed I can post the full stack trace,  but it is a "bit long".

    Sunday, March 18, 2012 9:28 PM

Answers

  • Entity Framework does no tracking on the results of a projection. Regarding the reasoning behind the decision to only support parameterless constructors you can see the following thread: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d700becd-fb4e-40cd-a334-9b129344edc9/

    The idea of supporting only parameter-less constructor was one of the hard decisions we made as a product team. The main idea with this approach was we shouldn't open up new surface area in LINQ over Entities that is not supported by EDM. EDM in general doesn't allow you to construct in random objects (using the NEW constructor in eSQL). To make it consistent with the whole stack, we decided to implement it like wise. Limiting the constructions only to:

    - Parameter less constructors

    - Anonymous types (as it’s the only way to do multi-project in LINQ over Entities)

    Regards,

    Tyler


    Monday, March 19, 2012 1:23 AM

All replies

  • Hi Aussie_Craig_Developer,

    As stated in the exception, LINQ to Entities queries can only be projected into objects with parameterless constructors. The problem is that XElement and XAttribute don't have parameterless constructors. To get around this you'll need to first project into an anonymous type and then use a LINQ to Objects query to convert it to your XML structure:

    var elements = (from artist in cont.Artists1
                              select new {
                                                    Name = artist.ArtistInfo.Title,
                                                    AlbumTitles = artist.Albums.Select(alb => alb.AlbumInfo.Title),;
                                                })
                   .ToList()//Load the data into memory and convert to LINQ to Objects
                   .Select(artist => 
                            new XElement("Artist", new XAttribute("Name", artist.Name),
                             artist.AlbumTitles.Select(title => new XElement("Album", new XAttribute("Album", title)))
                            );

    Regards,

    Tyler


    Sunday, March 18, 2012 10:32 PM
  • Tyler,

    If the behaviour is "as per design" then I can accept this as the answer.

    I must confess that it “smelt like a bug” from my perspective. It would seem, to me, that the EF code is “getting involved” with something which should not be part of its business. The only reason I can see for the EF “getting involved” is if the EF was going to try to change track into a type, or types not part of the EF generated support. It would seem to me a “bit of an over-reach”, if the EF is trying to “watch over” objects that are not strictly objects that came from the container (the DBContext derived class) or the EF generated classes

    Craig


    Monday, March 19, 2012 1:07 AM
  • Entity Framework does no tracking on the results of a projection. Regarding the reasoning behind the decision to only support parameterless constructors you can see the following thread: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d700becd-fb4e-40cd-a334-9b129344edc9/

    The idea of supporting only parameter-less constructor was one of the hard decisions we made as a product team. The main idea with this approach was we shouldn't open up new surface area in LINQ over Entities that is not supported by EDM. EDM in general doesn't allow you to construct in random objects (using the NEW constructor in eSQL). To make it consistent with the whole stack, we decided to implement it like wise. Limiting the constructions only to:

    - Parameter less constructors

    - Anonymous types (as it’s the only way to do multi-project in LINQ over Entities)

    Regards,

    Tyler


    Monday, March 19, 2012 1:23 AM