Code-Only Calculated Fields

4 posts, 0 answers
  1. Emily
    Emily avatar
    5 posts
    Member since:
    Nov 2015

    Posted 25 Nov 2015 Link to this post

    I am getting started with Data Access and I am trying to figure out how exactly one would go about creating the database from code when the classes you are mapping have calculated properties. For example, I have a class with the following property

    public double SpaceTotal
    {
        get { return Math.Round((double) spaceTotal/1073741824 - 0.005, 2); }
    }
     
    public double SpaceFree
    {
        get { return Math.Round((double)spaceFree/1073741824 - 0.005, 2); }
    }
     
    public double SpaceFreeAsPercentageOfSpaceTotal
    {
        get { return Math.Round(SpaceFree/SpaceTotal, 2) * 100.00; }
    }

    When I try to build my project I get the following error:

    There is no field with name 'spaceFreeAsPercentageOfSpaceTotal' backing 'SpaceFreeAsPercentageOfSpaceTotal' property in type 'MyFluentProject.Data.Type.VMDrive'. You need to either change the Field Naming rules of the mapping configuration object or call HasFieldName(string) with the name of the backing field.

     

    I am currently using default mapping and I am digging through the documentation for code-only development and I cannot find a code-only section that deals with setting up the database in-code from scratch when you have properties that are calculated using other properties of the same class.

     Any ideas?

  2. Yavor Slavchev
    Admin
    Yavor Slavchev avatar
    22 posts

    Posted 27 Nov 2015 Link to this post

    Hi Emily,

    In order to exclude these properties from the database mapping, you have to mark them as transient. Take a look at this article how to mark properties as transient using Code only mapping.

    I hope this information helps.

    Regards,
    Yavor Slavchev
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. Emily
    Emily avatar
    5 posts
    Member since:
    Nov 2015

    Posted 30 Nov 2015 in reply to Yavor Slavchev Link to this post

    Yavor,

     

    I think what I was asking was if there was a code-only way to set up the database such that a particular column is calculated instead of just 'set'.

     

    Example: If one of my columns is the size of a disk drive on a PC in bytes but I want to show it in the database as gigabytes, I have to divide by 1073741824 before it gets posted to the database. However, if I try to set the get method of that property as:

     

    get{ return size / 1073741824; }

    It throws exceptions when the database is being set up. Is the only way to do this to set up the database initially and then manually change the column to a calculated column in the database afterwards?

  5. Yavor Slavchev
    Admin
    Yavor Slavchev avatar
    22 posts

    Posted 01 Dec 2015 Link to this post

    Hi Emily,
    Thank you for your clarification.

    I can suggest you in this case to have two columns in the database, 'Size' and 'SizeInGb' for example and set the 'SizeInGb' value in the setter of the 'Size' columns. Something like this:
    private int size;
     
    public int SizeInGb { get; set; }
     
    public int Size
    {
        get
        {
            return this.size;
        }
        set
        {
            this.size = size;
            this.SizeInGb = this.size / 1073741824;
        }
    }

    Both properties should be persistent and mapped to the database. This way, you have to update only the 'Size' property that will automatically update the 'SizeInGb' in the database and you can then read the calculated value directly through the getter of the 'SizeInGb' property.

    I hope this inforamation is helpful.

    Regards,
    Yavor Slavchev
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top