0

Code First Class:

public class Product
{
  [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public int ProductId{ get; set; }
  [MaxLength(256), Required]
  public string ProductName { get; set; }
  public DateTime CreatedTime { get; set; }
}

In the database, i give the CreatedTime Column the DEFAULT value "(getdate())", so that the database will generate a value for us on insert operation.

My Question: Is there some kind of DataAnnotations/something else that can do the following things:

  1. can tell Entity Framework(Code First Mode) to include CreateTime when Building up the Database;
  2. can tell EF NOT to include CreateTime when Inserting a new column into the Database;
  3. can tell EF to retrieve back all the columns including CreateTime when Selecting from the database.

So that, I can treat the CreatedTime Column in the Client Side the same as the ProductId Column(which is the Identity Column: value Only can be generated by the Database; will NOT be included when Inserting a new column; will be included when Selecting...).

I think it's much more clear now, sorry for the misunderstanding!

Dean

tpeczek
  • 23,447
  • 3
  • 72
  • 76
Dean
  • 1,271
  • 3
  • 15
  • 22
  • Possible duplicate see http://stackoverflow.com/questions/3137738/ef4-code-first-how-to-set-default-value-for-pocos . Basically the conculsion seems ti be you should use database defaults beacsue if you do you aren't coding first. :( – Tony Hopkinson May 26 '12 at 10:49
  • Sorry that should be shoud not use. :( – Tony Hopkinson May 26 '12 at 11:04
  • I have edited the Question, that's not what i'm asking, sorry for the misunderstanding. – Dean May 26 '12 at 12:33
  • 1
    Seems to be the same question reworded. It's my understanding that "dynamic" defaults such as GetDate haven't been catered for and in fact won't be, because only the database can provide the date and only at the time it's inserted and that violates the underlying reasoning behind the framework. Maybe one of the others can help, but I've seen this question asked several ways ans answered in more, and every answer I've seen has been, no, can't be done. – Tony Hopkinson May 26 '12 at 13:16

0 Answers0