目录

文章列表:

  1. 创建EF数据模型
    Creating an Entity Framework Data Model
  2. 实现基础的CRUD功能
    Implementing Basic CRUD Functionality
  3. 排序、筛选和分页
    Sorting, Filtering, and Paging
  4. 连接恢复和命令拦截
    Connection Resiliency and Command Interception
  5. Code First 迁移和部署
    Code First Migrations and Deployment
  6. 创建更复杂的数据模型
    Creating a More Complex Data Model
  7. 读取相关数据
    Reading Related Data
  8. 更新相关数据
    Updating Related Data
  9. 使用异步和存储过程
    Async and Stored Procedures
  10. 处理应用程序并发
    Handling Concurrency
  11. 实现继承
    Implementing Inheritance
  12. 高级EF功能
    Advanced Entity Framework Scenarios

完整的数据模型

数据模型

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel.DataAnnotations;
 4 using System.ComponentModel.DataAnnotations.Schema;
 5 
 6 namespace ContosoUniversity.Models
 7 {
 8     public class Student
 9     {
10         public int ID { get; set; }
11         [Required]
12         [StringLength(50)]
13         [Display(Name = "Last Name")]
14         public string LastName { get; set; }
15         [Required]
16         [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
17         [Column("FirstName")]
18         [Display(Name = "First Name")]
19         public string FirstMidName { get; set; }
20         [DataType(DataType.Date)]
21         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
22         [Display(Name = "Enrollment Date")]
23         public DateTime EnrollmentDate { get; set; }
24 
25         [Display(Name = "Full Name")]
26         public string FullName
27         {
28             get
29             {
30                 return LastName + ", " + FirstMidName;
31             }
32         }
33 
34         public virtual ICollection<Enrollment> Enrollments { get; set; }
35     }
36 }

 

 1 using System.ComponentModel.DataAnnotations;
 2 
 3 namespace ContosoUniversity.Models
 4 {
 5     public enum Grade
 6     {
 7         A, B, C, D, F
 8     }
 9 
10     public class Enrollment
11     {
12         public int EnrollmentID { get; set; }
13         public int CourseID { get; set; }
14         public int StudentID { get; set; }
15         [DisplayFormat(NullDisplayText = "No grade")]
16         public Grade? Grade { get; set; }
17 
18         public virtual Course Course { get; set; }
19         public virtual Student Student { get; set; }
20     }
21 }

 

 1 using System.Collections.Generic;
 2 using System.ComponentModel.DataAnnotations;
 3 using System.ComponentModel.DataAnnotations.Schema;
 4 
 5 namespace ContosoUniversity.Models
 6 {
 7    public class Course
 8    {
 9       [DatabaseGenerated(DatabaseGeneratedOption.None)]
10       [Display(Name = "Number")]
11       public int CourseID { get; set; }
12 
13       [StringLength(50, MinimumLength = 3)]
14       public string Title { get; set; }
15 
16       [Range(0, 5)]
17       public int Credits { get; set; }
18 
19       public int DepartmentID { get; set; }
20 
21       public virtual Department Department { get; set; }
22       public virtual ICollection<Enrollment> Enrollments { get; set; }
23       public virtual ICollection<Instructor> Instructors { get; set; }
24    }
25 }

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel.DataAnnotations;
 4 using System.ComponentModel.DataAnnotations.Schema;
 5 
 6 namespace ContosoUniversity.Models
 7 {
 8     public class Instructor
 9     {
10         public int ID { get; set; }
11 
12         [Required]
13         [Display(Name = "Last Name")]
14         [StringLength(50)]
15         public string LastName { get; set; }
16 
17         [Required]
18         [Column("FirstName")]
19         [Display(Name = "First Name")]
20         [StringLength(50)]
21         public string FirstMidName { get; set; }
22 
23         [DataType(DataType.Date)]
24         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
25         [Display(Name = "Hire Date")]
26         public DateTime HireDate { get; set; }
27 
28         [Display(Name = "Full Name")]
29         public string FullName
30         {
31             get { return LastName + ", " + FirstMidName; }
32         }
33 
34         public virtual ICollection<Course> Courses { get; set; }
35         public virtual OfficeAssignment OfficeAssignment { get; set; }
36     }
37 }

 

 1 using System.ComponentModel.DataAnnotations;
 2 using System.ComponentModel.DataAnnotations.Schema;
 3 
 4 namespace ContosoUniversity.Models
 5 {
 6     public class OfficeAssignment
 7     {
 8         [Key]
 9         [ForeignKey("Instructor")]
10         public int InstructorID { get; set; }
11         [StringLength(50)]
12         [Display(Name = "Office Location")]
13         public string Location { get; set; }
14 
15         public virtual Instructor Instructor { get; set; }
16     }
17 }

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel.DataAnnotations;
 4 using System.ComponentModel.DataAnnotations.Schema;
 5 
 6 namespace ContosoUniversity.Models
 7 {
 8    public class Department
 9    {
10       public int DepartmentID { get; set; }
11 
12       [StringLength(50, MinimumLength=3)]
13       public string Name { get; set; }
14 
15       [DataType(DataType.Currency)]
16       [Column(TypeName = "money")]
17       public decimal Budget { get; set; }
18 
19       [DataType(DataType.Date)]
20       [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
21       [Display(Name = "Start Date")]
22       public DateTime StartDate { get; set; }
23 
24       public int? InstructorID { get; set; }
25 
26       public virtual Instructor Administrator { get; set; }
27       public virtual ICollection<Course> Courses { get; set; }
28    }
29 }
posted @ 2015-10-23 10:10  duanyong  阅读(205)  评论(0编辑  收藏  举报