dbcontext class 用来crud数据库,也用来配置实体类,关系映射,缓存,事务,变化追踪等
using System.Data.Entity;
public class SchoolContext : DbContext
{
public SchoolContext()
{
}
// Entities
public DbSet<Student> Students { get; set; }
public DbSet<StudentAddress> StudentAddresses { get; set; }
public DbSet<Grade> Grades { get; set; }
}
实体中有标准属性和导航属性(关系导航与集合导航),DbSet 实体集中有多个实体
DbSet为实体集,Student为实体,StudentID、StudentName等为标准属性,
Grade为导航属性。(通常与另一个实体有关系),此为关系导航属性。
ICollection如Grade中的ICollection为集合导航属性
public class SchoolContext : DbContext
{
public SchoolContext()
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Grade> Grades { get; set; }
}
public class Student
{
// scalar properties
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime? DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; }
//reference navigation property
public Grade Grade { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
//collection navigate property
public ICollection<Student> Students { get; set; }
}