ColumnAttribute - Expression - is it a way to define computed column in database.

10 posts, 0 answers
  1. Krasimir
    Krasimir avatar
    14 posts
    Member since:
    Feb 2009

    Posted 10 Dec 2010 Link to this post

    The OpenAccess documentation http://www.telerik.com/help/openaccess-orm/metadata-mapping-schemas-clr-attribute-column.html states that ColumnAttribute Expression named paramentes is used to indicate database computed column.

    However the  following creates regular db column despite of value of IsBackendCalculated (true / false / unspecified  )
    protected string test;
            [Telerik.OpenAccess.Column("test",Expression="id",IsBackendCalculated=true)]
    public string Test
    {
        get { return test;
        set { test = value; }
    }

    I was unable to find more info about Expression  and how to used it. What is the relation with  IsBackendCalculated.

    I saw that more of the other properties can be set via extension keys http://www.telerik.com/help/openaccess-orm/db-column.html to the artificial fields.
    Are there corresponding extension keys for Expression and IsBackendCalculated  or is there another way to define artificial field based on calculated column.

    Best regards

    Krasimir Filipov
     


     
  2. Serge
    Admin
    Serge avatar
    375 posts

    Posted 15 Dec 2010 Link to this post

    Hi Krasimir,

     The expression named parameter is currently not supported, we will be updating the on-line documentation. Thank you for noticing this. The backend calculated option is usually marked as true on columns that are auto incremented on the server such as identity columns. 

    You should also know that the extension keys are part of OpenAccess classic and these attributes are ones usually generated by the visual designer, they should not be mixed. 

    If you are trying to create a property that is calculated based on other properties in the class you should implement it by a read only property. Please give us a little more insight on what you are trying to accomplish with artificial fields so that we might suggest a different approach.

    I hope this helps.

    All the best,
    Serge
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  3. DevCraft banner
  4. Krasimir
    Krasimir avatar
    14 posts
    Member since:
    Feb 2009

    Posted 04 Jan 2011 Link to this post

    Hi Serge ,

    I'm still using OpenAccess classic approach  because as far as I know artificial fields and classes are only supported with this approach.

    I'm trying to implement some form of content management system that allow users to define structure of their items. Each type of items is mapped to an artificial type with each property mapped to an artificial field.
    Items use internal id strategy with autoinc generator .

    However I want to add ability to have cross items links. For visual representaion of thous links I plan to use something like multiple id fields keys used by Openaccess  - a dash separated item type id  and item id

    i.e  if "person" and "address" are two artificial classes
    person with id 5 will have unique id 1-5, person with id 345 will have uniqueid - 1-345
    and address with id 3   will have  unique id  2-3

    I was trying to introduce calculated field "uniqueID" based on formula "somenumber-" +[id]

    Any advice how to achieve this behavior without need to execute insert (in order to receive next autoincremented id  ) and then update operation to fiill the value  of calculated column will be highly appreciated.


    Best regards

    Krasimir Filipov  




     
     
     

  5. Serge
    Admin
    Serge avatar
    375 posts

    Posted 07 Jan 2011 Link to this post

    Hi Krasimir,

     I would like to mention that we have introduced a whole new Fluent Mapping API, that makes working with artificial fields and types really easy and removes the need of producing xml. With it you can also take a model created by the designer and extend it with artificial types and fields. If you want more information on the matter you can have a look at the fluent mapping chapters in the documentation or ask us directly. 

    We also support all kinds of associations with both artificial, normal and mixed types between them.  

    When working with artificial types, the identity is usually handled internally and there is always a field called id that serves as the identity for the type. There is little control for the end user on how the identity is generated. However given that you can always know the id of the type and its name it should be easy to implement some sort of outside mechanism that handles this matter.

    I hope this is helpful, however if not please shed some more light on what you are trying to achieve so that we can better help you.

    Regards,
    Serge
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  6. Krasimir
    Krasimir avatar
    14 posts
    Member since:
    Feb 2009

    Posted 07 Jan 2011 Link to this post

    Hi Serge,

    Thank you for mentioning Fluent Mapping API. I will take a look at the documentation in order to see how  to manage artificial classes with this approach

    According to what I try to achieve imagine class Foo with 3 properties ID (auto-increment ), UniqueID (read only string )
    , and Name (string)

    using ( SomeContext dbContext = new SomeContext () )
    {
       // Create a new foo
       Foo newFoo = new Foo();
       newFoo.Name = "New";
       dbContext.Add( newFoo );
       dbContext.SaveChanges();
    }

    As a result this create a record in foo table with id let say 5.
    I want the data to be stored also to include value for UniqueID equal to "SomeConstant-5" where 5 comes from the primary key of the item.

    If UnuqueId could be mapped to calculated field with formula 'SomeConstant-' +[id] this can be achieved in single call.

    Could you suggest how avoid subsequent update after initial insert in order to have value of UniqueID populated.

    Best regards
    Krasimir Filipov








  7. Serge
    Admin
    Serge avatar
    375 posts

    Posted 12 Jan 2011 Link to this post

    Hi Krasimir,

     I will suggest not persisting the UniqueId at all, just defining a get only property that calculates it on the client will do the trick, given that this is not an artificial type. 

    Another solution to your problem is to actually create a computed column in the database and map it to a read only property. (You can mark a property as a read only by selecting it on the diagram and specifying PersistentReadOnly as its kind). 

    In the scenario where these are artificial types and fields I am afraid you will not be able to work around the two updates.

    I hope this is helpful. 

    Kind regards,
    Serge
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  8. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 25 Jun 2011 Link to this post

    Hello Serge,

    You make it sound so easy... :-)

    I have a table Contact and a table Employee. In Employee there is a ContactID field, in Contact also, it's his pk. So in my model my Employee pers class has a "Contact" property with the contact data. I want a calculated field that shows FirstName + LastName to fill a RadComboBox.

    Well...
    1:
    Dim em As IQueryable = From c In Core.Connection.Application.Data.Employees Order By c.Contact.Name Select Value = c.EmployeeID, Text = LTrim(c.Contact.FirstName & " ") & c.Contact.Name
    rcmb_Managers.DataSource = em
    rcmb_Managers.DataBind()
    The RadCombo has this set: DataTextField="Text" DataValueField="Value" 
    Error. Execution of 'Microsoft.VisualBasic.Strings:LTrim(String)' on the database server side currently not implemented.
    Ok. Not possible. (I my case, there is also a MiddleName and a title with a propercase Fn.)
    Using a function also does not work.

    2: Ok, I will create a calculated property in Contact pers class:
    Public ReadOnly Property FullName As String
        Get
            If Me.ContactTypeCode = "C" Then
                Return ToProperCase((Me.Name & "").Trim & ", " & (LTrim(Me.FirstName) & "").Trim & " " & (Me.MiddleName & "")).Trim
            Else
                Return ToProperCase(Me.Name)
            End If
        End Get
    End Property
    Binding:
    Dim em As IQueryable = From c In Core.Connection.Application.Data.Employees Order By c.Contact.Name Select Value = c.EmployeeID, Text = c.Contact.FullName
    rcmb_Managers.DataSource = em
    rcmb_Managers.DataBind()

    Error: An exception occured during the execution of '
    Extent<AP.Data.Entities.Employee>.OrderBy(c => c.Contact.Name).Select(c => new VB$AnonymousType_0`2(Value = c.EmployeeID, Text = c.Contact.FullName))'. See InnerException for more details.


    Inner: Field 'FullName' not found on 'AP.Data.Entities.Contact'. If 'FullName' is a property please add the FieldAlias or Storage attribute to it or declare it as a field's alias.

    3: create the prop in Employee. That throws the same error as in 2.

    Using Telerik OA 2011.1.510.1, Net 4, VS2010, SQL Server Express

    So, just showing fullname became an adventure...

    Erik
  9. Serge
    Admin
    Serge avatar
    375 posts

    Posted 30 Jun 2011 Link to this post

    Hi Erik,

     Unfortunately at the moment the property you have added is treated as a persistent property and thus the problem. The LINQ translation cannot handle it on its own. However if you were to materialize the object in memory you will be able to do this, just perform a ToList() call prior to the Select statement. You will be able to specify exactly what you need retrieved using a FetchPlan

    I hope this helps. Please do not hesitate to let us know if you face further trouble.

    All the best,
    Serge
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
  10. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 30 Jun 2011 Link to this post

    Hello Serge,

    Thank you for your response.

    I've tried this:
    Imports Telerik.OpenAccess.FetchOptimization
     
    ..................
     
    Dim _fetchStrategy As New FetchStrategy()
    _fetchStrategy.LoadWith(Of Entities.Employee)(Function(c) c.Contact)
    Core.Connection.FetchStrategy = _fetchStrategy
    note the import! FetchStrategy is in the namespace Telerik.OpenAccess.FetchOptimization

    but still getting the error. Do I misunderstand?
  11. Serge
    Admin
    Serge avatar
    375 posts

    Posted 04 Jul 2011 Link to this post

    Hello Erik,

     Using a Fetch strategy will not solve your problem on its own, you also need to retrieve the object in memory by performing a to list operation prior to the projection. You will have to split your query a bit in order to do so. One retrieving the objects in memory and a select statement so that you can perform the actual projection and get the real results. 

    The fetch strategy will help with optimizing the query that retrieves the objects and this is why I mentioned it. 

    I hope this is helpful, however if you still have problems please let me know, I can create a sample application for you to use as a reference.

    Kind regards,
    Serge
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
Back to Top
DevCraft banner