Entity Sub Classes

Posts   
 
    
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 05-Mar-2009 20:44:59   

I'm having an issue. My issue looks this:

Unable to cast object of type 'Shivam.Horsell.Quotation.DAL.EntityClasses.CustomerQuotationEntity' to type 'Shivam.Horsell.Quotation.DAL.EntityClasses.MyCustomerQuotationEntity'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'Shivam.Horsell.Quotation.DAL.EntityClasses.CustomerQuotationEntity' to type 'Shivam.Horsell.Quotation.DAL.EntityClasses.MyCustomerQuotationEntity'.

Source Error:

Line 208: public new virtual MyCustomerQuotationEntity CustomerQuotationUsingCustomerQuotationId Line 209: { Line 210: get { return (MyCustomerQuotationEntity)base.CustomerQuotationUsingCustomerQuotationId; } Line 211: set { base.CustomerQuotationUsingCustomerQuotationId = value; } Line 212: }

When (using linq) i fetch my customerquotation entities I prefetch the ProductDetail entities. ProductDetailEntity is a super type of RestaurantProductDetailEntity.... maybe i would help if you saw the query :

var quotes = (from c in meta.CustomerQuotation
                              join o in meta.ProductDetail on c.CustomerQuotationId equals o.CustomerQuotationId
                              join z in meta.Product on o.ProductId equals z.ProductId
                              where z.SystemName == "Restaurant" && c.Version == meta.CustomerQuotation.Where(x => x.QuoteNo == c.QuoteNo).Max(p => p.Version)
                              select c).WithPath(opath => opath
                    .Prefetch<BrokerEntity>(p => p.BrokerUsingBrokerId)
                    .Prefetch<RestaurantProductDetailEntity>(p => p.ProductDetailUsingCustomerQuotationId).SubPath(spath => spath
                        .Prefetch<BuyingGroupEntity>(p=>p.BuyingGroupUsingBuyingGroupId)
                        .Prefetch<OccupationEntity>(p => p.OccupationUsingOccupationId)
                        .Prefetch<ProductEntity>(p => p.ProductUsingProductId)
                        )
                    ).ToList();

I get this error when i try and read the CustomerQuotationUsingCustomerQuotatationId property of the ProductEntity. I believe this happens when reading the property that returns the object that prefetched the object your trying to read the property from... get me? smile

In a nut shell the value of CustomerQuotationUsingCustomerQuotatationId ISNT MyCustomerQuotationEntity like it's expecting. Instead its just CustomerQuotationEntity and I can't see that this is the fault of anything I have done nor can I see any way to correct this issue. Smells like a bug.

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 05-Mar-2009 20:57:00   

...I can work around it by casting the the RestaurantProductDetail to ProductDetail so it doesn't call the "new" property. Shouldn't have to do this..

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 05-Mar-2009 21:06:44   

Hi

Please could you let us know which versions/settings you are using as outlined here ?

Have you tried updating to, and using the latest version ? Many small fixes have been made to the LINQ provider over the last few months - it's always worth trying the latest version simple_smile

Matt

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 06-Mar-2009 05:04:20   

Hi Matt,

I'm using llb 2.6. Updating is always my first attempt at fixing and I am using Feb 6th build which still seems to be the latest available. Tha'ts the Feb 6th designer and runtime.

Heres the full stack trace:

[InvalidCastException: Unable to cast object of type 'Shivam.Horsell.Quotation.DAL.EntityClasses.CustomerQuotationEntity' to type 'Shivam.Horsell.Quotation.DAL.EntityClasses.MyCustomerQuotationEntity'.] Shivam.Horsell.Quotation.DAL.EntityClasses.MyProductDetailEntity.get_CustomerQuotationUsingCustomerQuotationId() in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.Quotation.DAL\EntitySubClasses\MyProductDetailEntity.cs:210 Shivam.Horsell.Quotation.BL.Rater.RatingFigureCalculator.get_AdminFee() in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.Quotation.BL\Rater\RestaurantRatingEngine.cs:465 Shivam.Horsell.Quotation.BL.Reports.RestaurantMasterReport.<.ctor>b__45(RatingFigureCalculator p) in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.Quotation.BL\Reports\RestaurantMasterReport.cs:99 System.Linq.WhereSelectListIterator2.MoveNext() +113 System.Linq.Enumerable.Sum(IEnumerable1 source) +92 System.Linq.Enumerable.Sum(IEnumerable1 source, Func2 selector) +58 Shivam.Horsell.Quotation.BL.Reports.RestaurantMasterReport.<.ctor>b__7(<>f__AnonymousType24 c) in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.Quotation.BL\Reports\RestaurantMasterReport.cs:48 System.Linq.WhereSelectListIterator2.MoveNext() +107 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +7663156 System.Linq.Enumerable.ToList(IEnumerable`1 source) +61 Shivam.Horsell.Quotation.BL.Reports.RestaurantMasterReport..ctor(DateTime startDate, DateTime endDate) in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.Quotation.BL\Reports\RestaurantMasterReport.cs:47 Shivam.Horsell.WebUI.Biteme.Run(Object sender, EventArgs e) in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.WebUI\Biteme.aspx.cs:43 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

I'm using Adapter I'm using .Net 3.5SP1 I'm using SQL 2005 Standard Edition

Inheritence hierachy:

The entity that is throwing the error is part of an inheritence hierarchy which looks like this:

RestaurantProductDetail table inherits ProductDetail table.

I'm not custom templates but I am using a custom task performer than build only one dal project, so the database specific stuff is in the same library, otherwise it's the same as the vanilla performers.

I've already posted the linq query used to get the objects. I originally thought this error was only occurring for the CustomerQuotationUsingCustomerQuotationId but it's actaully happening when I try to read any of the prefetched objects from RestaurantProductDetailEntity (see the linq query).

There is no problem when I use the non-linq approach (adapter.FetchEntityCollection).

Heres an example of a line that calls the property where an exception is thrown: (note "productDetail" is of type RestaurantProductDetailEntity).


int rateVersion = productDetail.CustomerQuotationUsingCustomerQuotationId.RateVersion;

But this line works fine:


int rateVersion = ((ProductDetailEntity)productDetail).CustomerQuotationUsingCustomerQuotationId.RateVersion;

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 06-Mar-2009 08:25:09   

Try to use the "MyXXXEntity" in your prefetchPaths to define the type of the prefetched entity, rather than the baseClass.

A relevant thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7775

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 07-Mar-2009 10:15:11   

Ahh of course when i do the .Prefetch<typename> i should use the MyClass version. Now i feel stupid smile

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 08-Mar-2009 10:05:50   

Well I tried using the "My" classes in the prefetches for another report I am doing and it's not working.

My query:


var quotes = (from c in meta.CustomerQuotation
                              join o in meta.ProductDetail on c.CustomerQuotationId equals o.CustomerQuotationId
                              join z in meta.Product on o.ProductId equals z.ProductId
                              where c.QuotedDate >= startDate && c.QuotedDate <= endDate && z.SystemName == "Motel" && c.Version == meta.CustomerQuotation.Where(x => x.QuoteNo == c.QuoteNo).Max(p => p.Version)
                              select c).WithPath(opath => opath
                    .Prefetch<MyBrokerEntity>(p => p.BrokerUsingBrokerId)
                    .Prefetch<MyMotelProductDetailEntity>(p => p.ProductDetailUsingCustomerQuotationId).SubPath(spath => spath
                        .Prefetch<MyBuyingGroupEntity>(p => p.BuyingGroupUsingBuyingGroupId)
                        .Prefetch<MyMotelOccupationEntity>(p => p.OccupationUsingMotelOccupationId)
                        .Prefetch<MyProductEntity>(p => p.ProductUsingProductId)
                        )
                    ).ToList().Where(p => ValidateQuotation(p)).ToList();

My error:

[NullReferenceException: Object reference not set to an instance of an object.] SD.LLBLGen.Pro.LinqSupportClasses.PrefetchPathAPI.PathElementCreator.CreateEntityCollectionAdapter() +32 SD.LLBLGen.Pro.LinqSupportClasses.PrefetchPathAPI.PathElementCreator.CreatePrefetchElement() +256 SD.LLBLGen.Pro.LinqSupportClasses.PrefetchPathAPI.PathEdgeRootParser1.CreatePathEdge(Type source, Type destination, String propertyName) +47 SD.LLBLGen.Pro.LinqSupportClasses.PrefetchPathAPI.PathEdgeRootParser1.ParseComplexPathSpecification(MemberExpression expression) +113 SD.LLBLGen.Pro.LinqSupportClasses.PrefetchPathAPI.PathEdgeRootParser1.Prefetch(Expression1 expression) +107 Shivam.Horsell.Quotation.BL.Reports.MotelMasterReport.<.ctor>b__2(IPathEdgeRootParser1 opath) in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.Quotation.BL\Reports\MotelMasterReport.cs:32 SD.LLBLGen.Pro.LinqSupportClasses.QueryableExtensionMethods.WithPath(IQueryable1 source, Func`2 edgeSpecifierFunc) +110 Shivam.Horsell.Quotation.BL.Reports.MotelMasterReport..ctor(DateTime startDate, DateTime endDate) in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.Quotation.BL\Reports\MotelMasterReport.cs:28 Shivam.Horsell.AdminUI.Default.RunAccommodation(Object sender, EventArgs e) in D:\Projects\Horsell.Quotation\Horsell.Quotation\Shivam.Horsell.AdminUI\Default.aspx.cs:37 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

Any suggestions...?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 09-Mar-2009 07:40:04   

... .Prefetch<MyMotelProductDetailEntity>(p => p.ProductDetailUsingCustomerQuotationId).SubPath(spath => spath .Prefetch<MyBuyingGroupEntity>(p => p.BuyingGroupUsingBuyingGroupId) .Prefetch<MyMotelOccupationEntity>(p => p.OccupationUsingMotelOccupationId) .Prefetch<MyProductEntity>(p => p.ProductUsingProductId)

Is "MyMotelProductDetailEntity" of type "ProductDetailEntity"? Is "MyMotelOccupationEntity" of type "OccupationEntity"?

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 10-Mar-2009 08:32:24   

Yes & Yes

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 10-Mar-2009 11:05:16   

We need a repro case.

Please do the following: - your query is quite complex. try to remove the join and all prefetch path nodes which are unnecessary to reproduce the problem. I.o.w. if


var quotes = (from c in meta.CustomerQuotation
                             select c).WithPath(opath => opath
                    .Prefetch<MyBrokerEntity>(p => p.BrokerUsingBrokerId)
                    ).ToList()

gives the same problem, it's easier for us to track down. As you use both inheritance and the My* templates, it's a case where we need a custom repro case so we can directly investigate what's going on in your situation. You could setup the repro case with adventureworks on sqlserver if you like, it contains inheritance hierarchies as well (just auto-retrieve them).

If you manage to reproduce it with a query on adventureworks, please post that so we can try to repro it with that.

The crash occurs on this line:


return ((IElementCreator)_elementCreator).GetFactory(_destinationType).CreateEntityCollection();

so either _elementCreator is null (doubt it) OR the factory is likely not reproducable.

You could track if it's the latter by placing a breakpoint in the ElementCreator class in the EntityFactories.cs file. It has a GetFactory(Type) method, please set a breakpoint there and then run your query. The returned value should never be null. If it IS null, you can see for which type it can't find the factory and we can check from there what the problem might be.

Frans Bouma | Lead developer LLBLGen Pro
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 26-Mar-2009 05:06:11   

Hi,

is there some way to just disable the sub classes fvrom being generated? I dont' use them anyway... re pros are hard :|

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Mar-2009 06:04:12   

is there some way to just disable the sub classes fvrom being generated?

No, I'm afraid. The repro would be helpful to figure this out.

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 26-Mar-2009 10:34:44   

worldspawn wrote:

Hi,

is there some way to just disable the sub classes fvrom being generated? I dont' use them anyway... re pros are hard :|

Which subclasses? You opted for 2-class scenario I think. if you use the General preset instead, you won't get two-classes per entity and therefore won't have subclasses, but I'm guessing here if that's what you meant.

Frans Bouma | Lead developer LLBLGen Pro
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 27-Mar-2009 00:37:09   

Yeah I'm not sure how I did that. I have a lightly customised preset.

I went through my task preset and got rid of the entity sub class ones and changed any templates with the word "Derived" to .. the same word minus "Derived". Thanks for being consistent! Problem solved! Thanks.

Offtopic query: "Discovered" the TakePage method in linq yesterday and was looking at the sql it generated. Does SQL have no native way to return an ordered query starting from a specific index? I'm not that fussed just wondering. Looks like neat implementation if it doesn't, I wouldn't have thought of creating a temp table with an identity column.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Mar-2009 03:27:11   

worldspawn wrote:

I went through my task preset and got rid of the entity sub class ones and changed any templates with the word "Derived" to .. the same word minus "Derived". Thanks for being consistent! Problem solved! Thanks. [/quoted] Good it worked finally, Sam.

Offtopic query: "Discovered" the TakePage method in linq yesterday and was looking at the sql it generated. Does SQL have no native way to return an ordered query starting from a specific index? I'm not that fussed just wondering. Looks like neat implementation if it doesn't, I wouldn't have thought of creating a temp table with an identity column. Do you mean like a index hint? What DB are you planning to use this? Anyway, please open a new thread to treat that subject.

David Elizondo | LLBLGen Support Team