user5637268
user5637268

Reputation:

Set property to another property's value

I'm pretty new to this and my issue is very particular. I've googled a lot and haven't been able to find anything on this. I have two properties in a database, StartDate and StartMonth. The StartDate is of type DateTime and the StartMonth is of string type. (I know it's dumb to save both of these into the database but this isn't something I can change because I don't have the authority to do so.) My question is how in the DB Model can I set the StartMonth to equal the Month in the StartDate (since it is the same)? Rather, what is the proper way to do this? Any help is really appreciated!

To be clear I have currently:

[Table("EMPLOYMENT")]
public class Employment : EntityBase
{   
    [Column("STARTDATE")]
    public DateTime? StartDate { get; set; }

    [Column("STARTMONTH")]
    public string StartMonth { get; set; }
}

EDIT: I tried the following (I realize it wasn't any of the suggested, but it was suggested by a co-worker):

[Table("EMPLOYMENT")]
public class Employment : EntityBase
{   
    [Column("STARTDATE")]
    internal DateTime? StartDateInternal { get; set; }

    [Column("STARTMONTH")]
    private string StartMonth { get; set; }

    [NotMapped]
    public DateTime? StartDate
    {
        get
        {
            return StartDateInternal;
        }
        set
        {
            StartDateInternal = value;
            StartMonth = value?.ToString("MMMM");
        }
}

Specifically, the StartMonth is now private, the StartDate is now a NotMapped property that sets the StartDateInternal and StartMonth and returns the StartDateInternal value, and the original StartDate from the previous code is now named StartDateInternal. Though I can see that the value is being properly passed all the way to the model, it will not set the value. It always sets it to NULL in the database. I'm not sure why and was wondering if anyone could see why.

Upvotes: 2

Views: 8660

Answers (4)

Marius Orha
Marius Orha

Reputation: 670

You should not map StartMonth property, so it will not be saved in the database, but the model will still have the property. You can do something like:

    [NotMapped]
    public string StartMonth
    {
        get
        {
            return StartDate?.Month.ToString();
        }
    }

where NotMapped attribute tells EF to not map this property, but you are able to use this property in code. Also, you won't need a set property, because StartMonth will just return StartDate month. This way will make you sure that StartMonth and StartDate.Month doesn't have different values.

Upvotes: 0

BigTFromAZ
BigTFromAZ

Reputation: 776

Can you alter the database design?

If so, use a trigger or other facility in your database to render the STARMONTH column as a pseudo-column returning the "month" value of the STARTDATE column.

If not, then you can set the StartMonth when processing a new StartDate:

    [Table("EMPLOYMENT")]
    public class Employment
    {
        private DateTime _StartDate;

        [Column("STARTDATE")]
        public DateTime StartDate
        {
            get
            {
                return _StartDate;
            }
            set
            {
                _StartDate = value;
                StartMonth = value.ToString("MMMM") ?? null;

            }
        }

        [Column("STARTMONTH")]
        public string StartMonth { get; set; }

    }

A database pseudo column would guarantee a consistent result to all DB clients. The above code will be fine for anything that uses this code.

Upvotes: 0

Win
Win

Reputation: 62260

You can use a backing field, and get the month from StartDate only if StartMonth value is not set.

private string _startMonth;

[Column("STARTMONTH")]
public string StartMonth
{
    get
    {
        if (string.IsNullOrWhiteSpace(_startMonth) && StartDate.HasValue)
            return StartDate.Value.ToString("MMMM");
        return _startMonth;
    }
    set { _startMonth = value; }
}

Upvotes: 1

dana
dana

Reputation: 18125

Mark your setters as private, then create a SetStartDate method that sets both of the properties.

[Table("EMPLOYMENT")]
public class Employment : EntityBase
{   
    [Column("STARTDATE")]
    public DateTime? StartDate { get; private set; }

    [Column("STARTMONTH")]
    public string StartMonth { get; private set; }

    public string SetStartDate(DateTime? startDate)
    {
        this.StartDate = startDate;
        this.StartMonth = startDate.HasValue ? startDate.Value.ToString("MMMM") : null;
    }
}

Another way to model this is to mark the setter for StartMonth as private and have the setter for StartDate set both properties:

[Table("EMPLOYMENT")]
public class Employment : EntityBase
{   
    DateTime? startDate;

    [Column("STARTDATE")]
    public DateTime? StartDate
    {
        get { return this.startDate; }
        set
        {
            this.startDate = value;
            this.StartMonth = value.HasValue ? value.Value.ToString("MMMM") : null;
        }
    }

    [Column("STARTMONTH")]
    public string StartMonth { get; private set; }
}

Upvotes: 0

Related Questions