mds
mds

Reputation: 75

Override get, set in Entity Framework Database First

I have class from model first (EF 4.4 .Net 4.0):

public partial class Test
{
    public int Id {get; set; } 
    public int Date { get; set; }
    //other fields...
}

I can't change database model but I need to override get and set in this class. Something like that:

 [MetadataType(typeof(TestMetadata))]
 public partial class Test
 {
     public class TestMetadata
     {
         private int data;
         public DateTime Date
         {
            get
            {
                return DateTime.Today.Date;
            }
            set
            {
                date = value.Day;
            }
        }
    }

This approach dosen't work. Is it any possibilities to override get set in partial calss?

Upvotes: 3

Views: 5331

Answers (2)

mds
mds

Reputation: 75

My workaround. Something like that:

public partial class Test
    {
        public DateTime DateSi
        {
            get
            {
                return ConvertIntToDate(Date)
            }
            set
            {
                Date = ConvertDateToInt(value);
            }
        }
    }

In C# I use Test.DateSi (not mapped) and EF save into Database field Date with proper Int value. I only have to remember to use in C# DateSi instead of Date.

Upvotes: 2

Krzysztof Madej
Krzysztof Madej

Reputation: 40643

Here is a part of solution. I dont understand where (what property) do you want to save converted date. There is no chance to save int to datetime field.

public partial class Test
{
    string format = "yyy/mm/dd";

    public string DateFormat { get { return Date.ToString(format); }
}

Upvotes: 0

Related Questions