This is a migrated thread and some comments may be shown as answers.

Code-Only Calculated Fields

3 Answers 107 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Emily
Top achievements
Rank 1
Emily asked on 25 Nov 2015, 04:52 PM

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?

3 Answers, 1 is accepted

Sort by
0
Yavor Slavchev
Telerik team
answered on 27 Nov 2015, 12:17 PM
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.
0
Emily
Top achievements
Rank 1
answered on 30 Nov 2015, 04:56 PM

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?

0
Yavor Slavchev
Telerik team
answered on 01 Dec 2015, 04:10 PM
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.
Tags
Getting Started
Asked by
Emily
Top achievements
Rank 1
Answers by
Yavor Slavchev
Telerik team
Emily
Top achievements
Rank 1
Share this question
or