none
Using EF4.1 codefirst, is there anyway to mark a property as "computed only" RRS feed

  • Question

  • That is, Say I have a Person class that I've created with codefirst that has a person's birthdate in it.  I'd like to return a property with a get only that returns the number of days old that person is.  I don't want the column created in the database though.  How can I do that?

    Thanks


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Sunday, October 23, 2011 4:19 AM

Answers

  • Hi

    Are you looking the way not to map to DB? You can mark property with NotMapped attribute

     public class Student
        {
            [Key]
            public int StudentId { get; set; }
            public DateTime DateOfBirth { get; set; }
    
            [NotMapped]
            public int DOBInNumberOfDays {
                get
                {
                    //have a logic returns number of days from DOB
                }
            }
        }



    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Peter Kellner Sunday, October 23, 2011 4:30 AM
    Sunday, October 23, 2011 4:28 AM

All replies

  • Hi

    Are you looking the way not to map to DB? You can mark property with NotMapped attribute

     public class Student
        {
            [Key]
            public int StudentId { get; set; }
            public DateTime DateOfBirth { get; set; }
    
            [NotMapped]
            public int DOBInNumberOfDays {
                get
                {
                    //have a logic returns number of days from DOB
                }
            }
        }



    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Peter Kellner Sunday, October 23, 2011 4:30 AM
    Sunday, October 23, 2011 4:28 AM
  • thanks! just what I was looking for.  BTW, the reverse to codefirst with EF Power Tools CTP1 is awesome! did it on a database with lots of foreign keys and indexes.  The code is gorgeous!!! can't wait for it to go live.

    http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

     


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Sunday, October 23, 2011 4:31 AM