简体   繁体   中英

Convert date in Domain class that is created by EF6

I create a model using EF6 ,one of my entities as you can see here :

public partial class Comment
    {
        public Comment()
        {
            this.Points = new HashSet<Point>();
        }

        public int Id { get; set; }
        public int IdeaId { get; set; }
        public System.DateTime Date { get; set; }
        public string JurorId { get; set; }
        public string CommectDesc { get; set; }
        public string Point { get; set; }

        public virtual Idea Idea { get; set; }
        public virtual ICollection<Point> Points { get; set; }
    }

Can i convert date column using get{} in this class?I mean i need to converted my date ?because i save the date using datetime.now but when i want to show the date to user i have to convert it to persian date.

it's better to create new propery and decorate it with [System.ComponentModel.DataAnnotations.Schema.NotMapped] Attribute, Like this

public DateTime Date { get; set; }


[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public DateTime PersianDate
    {
        get
        {
                var value  = ConvertToHijri(Date);

                return (value);
        }
    }

not you have extra field that not mapped to your database, when you want to show datetime in hijri format, you can use PersianDate property instead of Date Property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM