sashomaga

Navigation property -> Foreign Key

Aug 5th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1.     class Category
  2.     {
  3.         private ICollection<Post> posts;
  4.  
  5.         public Category()
  6.         {
  7.             this.posts = new HashSet<Post>();
  8.         }
  9.  
  10.         public int CategoryId { get; set; }
  11.  
  12.         //virtual -> lazy loading Entity Framework
  13.         public virtual ICollection<Post> Posts
  14.         {
  15.             get { return this.posts; }
  16.  
  17.             set { this.posts = value; }
  18.         }
  19.  
  20.     }
  21.  
  22.     class Post
  23.     {
  24.     //primary key -> automatic set by Entity Framework id or class name + id
  25.         public int PostId { get; set; }
  26.  
  27.         public string Title { get; set; }
  28.  
  29.         public string Content { get; set; }
  30.  
  31.         //foreign key -> automatic set by Entity Framework
  32.         public int CategoryId { get; set; }
  33.  
  34.         //navigation property
  35.         public virtual Category Category { get; set; }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment