Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Linq2Sql - Explicit Construction of Entity Exception

Gotta say, I am kinda flustered right now.  Today I was trying to perform what I thought to be a pretty straight forward Linq2Sql query, but turns out it was not.

Overview

I have a query that joins a few tables together to get some data, but in place of returning explicit instance of a data entity, I wanted to do a select new XXXX.  I wanted to do this because I had extended the main entity to have a extra property that is needed based on my domain.  And because I did NOT want to create a who new entity, I thought this was the path with the least resistance.  WRONG.

Here is what I WANTED to do.

var items = ( from ctl in DBContextInstance().CampaignTrackingLinks
         join cts in DBContextInstance().CampaignTrackingSummaries on ctl.CampaignTrackingSummaryID equals cts.ID
         join ctcts in DBContextInstance().CampaignTrackingClickThroughStats on cts.ID equals ctcts.CampaignTrackingSummaryID
         where cts.SendID == sendID
         where ctl.Url == ctcts.URL
         select new CampaignTrackingLink
         {
             TotalClicks = ctl.TotalClicks,
             UniqueClicks = ctl.UniqueClicks,
             Url = ctl.Url,
             Alias = ctl.Alias
             PercentOfClicks = ctcts.PercentOfClicks
         }
       ).ToList();

However, every time I ran this code I received  the following exception 'Explicit construction of entity type 'XXXXX.CampaignTrackingLink' in query is not allowed.'.  What was throwing me at first was I KNEW that my syntax was correct, but it still did not work.

After a bit of googling I found (here) something that I think may explain why this is happening. 

The solution to get this to work was not to create a new instance of my extended Linq entity, but to create a whole new entity that inherits off of my Linq entity.  For some reason this worked

    public class CampaignTrackingLinkBreakdown : CampaignTrackingLink
    {
        public double PercentOfClicks { get; set; }
    }

It really sucks that I needed to create a new class that inherited off my Linq entity, but you do what you have to get it working.

var items = ( from ctl in DBContextInstance().CampaignTrackingLinks
         join cts in DBContextInstance().CampaignTrackingSummaries on ctl.CampaignTrackingSummaryID equals cts.ID
         join ctcts in DBContextInstance().CampaignTrackingClickThroughStats on cts.ID equals ctcts.CampaignTrackingSummaryID
         where cts.SendID == sendID
         where ctl.Url == ctcts.URL
         select new CampaignTrackingLinkBreakdown
         {
             TotalClicks = ctl.TotalClicks,
             UniqueClicks = ctl.UniqueClicks,
             Url = ctl.Url,
             Alias = ctl.Alias,
             PercentOfClicks = ctcts.PercentOfClicks
         }
       ).ToList();

So as you can see, I get the same end result, but have to jump through a few extra hoops.

Based on the feedback from the forum link above, this issue is due to the fact that Linq attempts to keep track of all entities for change state reasons and allowing for explicit construction via a query could cause a caching issue.  In my situation, I DO NOT care about change state as this is a read only entity so there will be no updates/inserts done later.

Oh well, at least now I know the issue and how to work around it.

If anyone knows of a better/cleaner way to solve this issue, please let me know.

Till next time,


Posted 04-25-2008 10:38 AM by Derik Whittaker
Filed under:

[Advertisement]

Comments

Darrell Mozingo wrote re: Linq2Sql - Explicit Construction of Entity Exception
on 04-25-2008 1:55 PM

I don't think you necessarily have to inherit from the LINQ entity to get this to work, as I load my DTOs in a similar fashion and they're just POCOs.

Derik Whittaker wrote re: Linq2Sql - Explicit Construction of Entity Exception
on 04-25-2008 2:01 PM

@Darrell,

You are 100% correct.  In my case I did not even want a POCO.  I only inherited it because i did not want to reinvent that whell.

There are other places where i create a POCO

Nick wrote re: Linq2Sql - Explicit Construction of Entity Exception
on 04-25-2008 2:29 PM

What if you set the ObjectTracking property on the DataContext to false?

Dew Drop - April 26, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - April 26, 2008 | Alvin Ashcraft's Morning Dew
on 04-26-2008 5:29 PM

Pingback from  Dew Drop - April 26, 2008 | Alvin Ashcraft's Morning Dew

Josh wrote re: Linq2Sql - Explicit Construction of Entity Exception
on 06-18-2008 8:09 PM

Hello,  I am trying to do the same thing but return an IQueryable<T>.  It is not working for me though.  Is there anything special I need to do to make that happen?  Is it possible?

<code>

public static IQueryable<Case> GetAll()

{

         var q = from o in db.Cases

                   join c in db.Customers on o.Id equals c.CaseId

                   select new Case

                   {

                    ...

                   };

           return q;

}

</code>

any help would be great.

Dave McAlister wrote re: Linq2Sql - Explicit Construction of Entity Exception
on 10-22-2008 5:11 AM

Make sure the entity you want to create has no primary key (in the designer) and then it works just fine.

Ken wrote re: Linq2Sql - Explicit Construction of Entity Exception
on 07-23-2009 5:08 AM

Dave...

I was having a good progress day today then I fine tuned the Primary keys on some tables and propagated the same to my Linq classes.  After a couple hours I suspected my system was showing signs of AI but, replace Intelligence with Stupid.  It must have figured out how to change the code because I was rolling back...back...back then I found  your short and to the point about "no Primary keys" in the Linq entities.  Thank You :)

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)