sashomaga

Data Annotations ->Mapping -> Entity Framework

Aug 7th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4.  
  5. namespace ConsoleBlog.Models
  6. {
  7.     public class Post
  8.     {
  9.  
  10.         private ICollection<PostAnswer> answers;
  11.         private ICollection<Tag> tags;
  12.  
  13.         public Post()
  14.         {
  15.             this.answers = new HashSet<PostAnswer>();
  16.             this.tags = new HashSet<Tag>();
  17.         }
  18.  
  19.         //primary key
  20.         [Key]
  21.         public int PostId { get; set; }
  22.  
  23.         [Required] //set to not nulluble
  24.         public string Title { get; set; }
  25.  
  26.         [Column("New Name")]
  27.         public string Content { get; set; }
  28.  
  29.         [NotMapped]
  30.         public DateTime CreatedOn { get; set; }
  31.  
  32.         //foreign key
  33.         [ForeignKey("Category")]
  34.         public int CategoryId { get; set; }
  35.  
  36.         //navigation property
  37.         public virtual Category Category { get; set; }
  38.  
  39.         public virtual ICollection<PostAnswer> Answers
  40.         {
  41.             get { return this.answers; }
  42.  
  43.             set { this.answers = value; }
  44.         }
  45.  
  46.         public virtual ICollection<Tag> Tags
  47.         {
  48.             get { return this.tags; }
  49.  
  50.             set { this.tags = value; }
  51.         }
  52.  
  53.         public PostType Type { get; set; }
  54.  
  55.         [Timestamp]
  56.         public byte[] RowVersion { get; set; }
  57.  
  58.         //includes propertiest from this class(class have [ComplexType])
  59.         public PostDetail Detail { get; set; }
  60.  
  61.     }
  62. }
  63.  
  64. namespace ConsoleBlog.Models
  65. {
  66.     [ComplexType]
  67.     public class PostDetail
  68.     {
  69.         public string CreatedByUser { get; set; }
  70.  
  71.         public bool IsDeleated { get; set; }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment