Adding custom fields at runtime problem (bug?)

Posts   
 
    
dcarapic
User
Posts: 60
Joined: 21-Dec-2007
# Posted on: 16-Apr-2013 11:24:35   

By following the example made by Mr. Bouma at http://weblogs.asp.net/fbouma/archive/2006/06/09/LLBLGen-Pro-v2.0-with-ASP.NET-2.0.aspx (step 6.) I've managed to create a custom entity factory where I can add my own fields which will get returned by a select query. I do however have a problem that the generated select query is not correct. I define my own custom field which is actually a sub-query. It would all work well if inside the sub-query LLBLGen would use the same table alias as in the outer query.

Here is the factory:


    Public Class ViewAssetEntityExtendedFactory
        Inherits ViewAssetEntityFactory

        Public Overrides Function CreateFields() As SD.LLBLGen.Pro.ORMSupportClasses.IEntityFields2
            Dim fields As SD.LLBLGen.Pro.ORMSupportClasses.IEntityFields2 = MyBase.CreateFields()
            fields.Expand(1)
            Dim f As IEntityField2 = New EntityField2("Attribute1", New ScalarQueryExpression(AssetAtributeValueFields.AttributeValue, ViewAssetFields.Id = AssetAtributeValueFields.AssetId And AssetAtributeValueFields.AssetTypeAttributeId = 671), GetType(DateTime))
            fields.DefineField(f, fields.Count - 1)
            Return fields
        End Function

    End Class

And here is the generated query (I've beautified it to be more readable):


    Query: 

SELECT TOP(@p3) [LPA_L1].[AREA_ID]           AS [AreaId]
                , [LPA_L1].[AREA_NAME]       AS [AreaName]
                , [LPA_L1].[BDE_AVAIL]       AS [BdeAvail]
                , [LPA_L1].[CC_ID]           AS [CcId]
                , [LPA_L1].[CC_INFO]           AS [CcInfo]
                , [LPA_L1].[COMMENT]           AS [Comment]
                , [LPA_L1].[COMPANY_ID]     AS [CompanyId]
                , [LPA_L1].[COMP_NAME]       AS [CompName]
                , [LPA_L1].[COSTCENTER]     AS [Costcenter]
                , [LPA_L1].[COSTCENTER_ID]   AS [CostcenterId]
                , [LPA_L1].[DESCRIPTIVE_FIELD] AS [DescriptiveField]
                , [LPA_L1].[ELECTRICAL_DATA]   AS [ElectricalData]
                , [LPA_L1].[FULL_PATH]       AS [FullPath]
                , [LPA_L1].[ID]             AS [Id]
                , [LPA_L1].[IMAGE]           AS [Image]
                , [LPA_L1].[LC_HIERARCHY_ID]   AS [LcHierarchyId]
                , [LPA_L1].[LC_LOCATION_ID] AS [LcLocationId]
                , [LPA_L1].[NAME]             AS [Name]
                , [LPA_L1].[ORDER_NO]         AS [OrderNo]
                , [LPA_L1].[PARENT_ASSET_ID]   AS [ParentAssetId]
                , [LPA_L1].[SHOW_STATISTIC] AS [ShowStatistic]
                , [LPA_L1].[SHOW_TRAFFIKLIGHT] AS [ShowTraffiklight]
                , [LPA_L1].[STATUS_IMAGE]     AS [StatusImage]
                , [LPA_L1].[TYPE_ID]           AS [TypeId]
                , [LPA_L1].[TYPE_NAME]       AS [TypeName]
                , [LPA_L1].[WORKPLACE_NO]     AS [WorkplaceNo]
                , [LPA_L1].[WORK_STATUS_ID] AS [WorkStatusId]
                , (SELECT [dbo].[ASSET_ATRIBUTE_VALUES].[ATTRIBUTE_VALUE] AS
                          [AttributeValue]
                   FROM   [dbo].[ASSET_ATRIBUTE_VALUES]
                   WHERE  ( [dbo].[VIEW_ASSETS].[ID] =
                            [dbo].[ASSET_ATRIBUTE_VALUES].[ASSET_ID]
                            AND
                  [dbo].[ASSET_ATRIBUTE_VALUES].[ASSET_TYPE_ATTRIBUTE_ID] = @p1
                          ))
                               AS [Attribute1]
FROM   [dbo].[VIEW_ASSETS] [LPA_L1]
ORDER  BY [LPA_L1].[ID] ASC 
    Parameter: @p1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 671.
    Parameter: @p3 : Int64. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 128.

Note that the outer table has alias [LPA_L1] while the subquery: (SELECT [dbo].[ASSET_ATRIBUTE_VALUES].[ATTRIBUTE_VALUE] AS [AttributeValue] FROM [dbo].[ASSET_ATRIBUTE_VALUES] WHERE ( [dbo].[VIEW_ASSETS].[ID] = [dbo].[ASSET_ATRIBUTE_VALUES].[ASSET_ID] AND [dbo].[ASSET_ATRIBUTE_VALUES].[ASSET_TYPE_ATTRIBUTE_ID] = @p1)) AS [Attribute1] does not have it.

Am I missing something here? Is there maybe a working alternative?

Using LLBLGen Pro 3.1 Final (September 30th, 2011), MS SQL provider, MS SQL Express 2008R2.

P.S.: The query is actually generated by Devexpress XtraGrid through LinqMetaData adapter but I do not think it should affect anything.

dcarapic
User
Posts: 60
Joined: 21-Dec-2007
# Posted on: 16-Apr-2013 12:10:06   

I've just realized that this thread contains a possible solution: https://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=14110&HighLight=1

However the provided code does not work. It would work if EntityField2.SetObjectAlias were virtual but sadly it is not.

Any ideas if this could be done in some other way?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Apr-2013 09:31:42   

I can't reproduce it using similar code against Northwind DB.

  • Could you write a repro case using Norhtwind?
  • How looks the code where you are actually fetching the collection?
  • Is that entity part of an inheritance hierarchy?
  • Is that entity mapped to a view target?

Also make sure you are using the latest LLBLGen runtime library version (ORMSupportClasses: 3.1.12.1015).

David Elizondo | LLBLGen Support Team
dcarapic
User
Posts: 60
Joined: 21-Dec-2007
# Posted on: 17-Apr-2013 09:41:32   
  1. I am going to try to make a small project with Northwind.
  2. The fetching is done automaticaly by LinqInstantFeedbackSource provided by DevExpress which enables async fetching of data for their grid. Here is the code that I wrote if it helps

    Private Sub dsAssets_GetQueryable(sender As System.Object, e As DevExpress.Data.Linq.GetQueryableEventArgs) Handles dsAssets.GetQueryable
        If _linq Is Nothing Then
            _linq = New Test.Linq.LinqMetaData(DataAccessAdapter.Current)
        End If
        e.QueryableSource = _linq.ViewAsset
    End Sub

  1. No, the entity is mapped to a db view without any hierarchy
  2. I do not know what you mean by "view target". In the database there is a view VIEW_ASSETS which I took over as an entity in LLBLGen. I did not do anything special with it.

Edit: ORM and Linq support classes are v3.1.0.0. I am going to get the latest version and try it again.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 17-Apr-2013 18:14:01   

I'll temporarily close this thread, it will be reopened once you post on it.

Posts: 26
Joined: 29-Sep-2011
# Posted on: 22-May-2013 01:04:39   

Is it possible to use "sysdate" field in a insert statement.

UPDATE UI_USER" SET "MDFCTN_DT" = SYSDATE , "UPDATE_NU" = 1, "PREV_LOGIN_DT" = SYSDATE WHERE ( (UI_USER"."USER_ID" = 'staff') AND ( (UI_USER"."UPDATE_NU" = 1)))

Currently we are using server time during insert. We have 10 servers and the servers times are getting out of sync and that's causing issue with our date fields, which are our audit fields. To fix this I was thinking to pass "sysdate" variable during insert so it will always take oracle server time.

So far I couldn't figure out how to do it using expressions.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-May-2013 08:25:06   

Hi Sunil,

You can use DBFunctionCalls with constants.

This is an example about that: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=14726&StartAtMessage=0&#82071

I think that, if you are using an older LLBLGen version (i.e. < v3), you should use a function wrapper in your DB and then call it with DBFunctionCall. The reason is that SYSDATE is not really a function, but a variable. See this: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=14726&StartAtMessage=0&#82229

Also, you mentioned "insert". Expressions (thus DBFunctionCalls) are not supported on inserts. So you should do it as an UPDATE. More info: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=14726&StartAtMessage=0&#82229

An alternative is to use a DB trigger, or a default value on your DB.

(P.D. Next time please open a new thread, this is mentioned in the forum guidelines: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7722 thanks simple_smile )

David Elizondo | LLBLGen Support Team