Assigning [NotMapped] property value after "regular" (mapped) property value has been assigned

uplink

Is there a way to assign a value to a not mapped property after all mapped properties get their values from the DB? For example:

public class ProcessSupervisor
{
    public int ID { get; set; }
    public int MaximumConcurrentProcesses { get; set; }

    [NotMapped]
    public int AvailableProcessSlots { get; set; }
}

I would like to keep the value of MaximumConcurrentProcesses in the database and assign that value to AvailableProcessSlots as a default value once Entity Framework instantiates ProcessSupervisor DBSet and assigns corresponding values to objects properties. Obviously I cannot assign the value of MaximumConcurrentProcesses to AvailableProcessSlots in class constructor since MaximumConcurrentProcesses hasn't received it's value from database yet. Is there a proper way do do it without initializing the AvailableProcessSlots value in some weird and awkward way?

bruno.almeida

You can try something like this:

public class ProcessSupervisor
{
    public int ID { get; set; }
    public int MaximumConcurrentProcesses { get; set; }

    [NotMapped]
    private int? availableProcessSlots = null;
    public int AvailableProcessSlots
    {
        get
        {
            return availableProcessSlots ?? MaximumConcurrentProcesses;
        }
        set
        {
            availableProcessSlots = value;
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Valid way to check that a double has been assigned a value of 0.0

Assigning a random enum value a property

SASS: Conditional values of CSS property based on that if an element with certain value of the same property has been present already in the page

'this' property has value undefined on nodeJS

Cypher query: Assigning an aggregate value as a property of a relationship

Property has a value but cannot select it

Error:No value has been specified for property 'checkstyleClasspath'

It is possible to query a NotMapped property?

Computed property was assigned to but it has no setter

WPF - NotMapped annotation in EF6 not storing / saving property value(s)

including a notmapped property

C# assigning value to property of struct error

After validation a field, property has last valid value

Reactjs useEffect to check whether there has been a change to an object's property's value

TimePicker value not changing property after loading value

ios magicalRecord not assigning string value to property

C# Restore a form property value that has been changed

The ConnectionString property has not been initialized after "using"

Set the SelectedIndex property of a ListView just after it has been populated

object property nil despite assigning him value

What is the value of a JFrame after it has been disposed?

Generic test in TypeScript that asserts each property on a class has a value assigned

SpotBugs "No value has been specified for property 'spotbugsClasspath'"

assigning undefined to JSON property value

How to ensure that an element in the IntArray has not been assigned a value in Kotlin?

Check if an int property has value assigned to it coming from an HTTP request

Why is a variable being assigned to a value AFTER the value has been modified when I've assigned it before it's been modified

How to check if a variable has been assigned a value in AutoHotkey?

How to get property value assigned in function definition to property decorator