将属性设置为另一个属性的值

时间:2020-12-27 21:06:53

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!

我是新手,我的问题非常特别。我在谷歌上搜了很多次,都找不到任何相关信息。我在数据库中有两个属性,StartDate和StartMonth。StartDate类型为DateTime, StartMonth类型为string。(我知道把这两个都保存到数据库中是愚蠢的,但这不是我可以改变的,因为我没有权限这么做。)我的问题是如何在DB模型中设置StartMonth等于StartDate中的Month(因为它是相同的)?相反,正确的做法是什么?非常感谢您的帮助!

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.

具体来说,StartMonth现在是私有的,StartDate现在是一个notmapping属性,它设置StartDateInternal和StartMonth,并返回StartDateInternal值,而来自前一段代码的原始StartDate现在被命名为StartDateInternal。虽然我可以看到该值被正确地传递到模型,但它不会设置值。它总是将其设置为数据库中的NULL。我不知道为什么,我想知道是否有人能看到为什么。

4 个解决方案

#1


1  

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

您可以使用一个背景字段,并且只有在没有设置StartMonth值时才能从StartDate获取月份。

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; }
}

#2


0  

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

将setter标记为private,然后创建一个SetStartDate方法来设置这两个属性。

[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:

另一种建模方法是将StartMonth的setter标记为private,并将StartDate的setter设置为两个属性:

[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; }
}

#3


0  

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.

如果是,请使用数据库中的触发器或其他工具将STARMONTH列呈现为返回STARTDATE列的“month”值的伪列。

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

如果没有,那么您可以在处理新的StartDate时设置StartMonth:

    [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.

数据库伪列将保证所有DB客户机得到一致的结果。以上代码对于使用此代码的任何代码都是适用的。

#4


0  

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:

您不应该映射StartMonth属性,因此它不会保存在数据库中,但是模型仍然具有该属性。你可以这样做:

    [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.

notmapping属性告诉EF不要映射这个属性,但是您可以在代码中使用这个属性。另外,您不需要设置属性,因为StartMonth将返回StartDate month。这样你就可以确定从月到月。月没有不同的值。

#1


1  

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

您可以使用一个背景字段,并且只有在没有设置StartMonth值时才能从StartDate获取月份。

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; }
}

#2


0  

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

将setter标记为private,然后创建一个SetStartDate方法来设置这两个属性。

[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:

另一种建模方法是将StartMonth的setter标记为private,并将StartDate的setter设置为两个属性:

[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; }
}

#3


0  

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.

如果是,请使用数据库中的触发器或其他工具将STARMONTH列呈现为返回STARTDATE列的“month”值的伪列。

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

如果没有,那么您可以在处理新的StartDate时设置StartMonth:

    [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.

数据库伪列将保证所有DB客户机得到一致的结果。以上代码对于使用此代码的任何代码都是适用的。

#4


0  

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:

您不应该映射StartMonth属性,因此它不会保存在数据库中,但是模型仍然具有该属性。你可以这样做:

    [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.

notmapping属性告诉EF不要映射这个属性,但是您可以在代码中使用这个属性。另外,您不需要设置属性,因为StartMonth将返回StartDate month。这样你就可以确定从月到月。月没有不同的值。