近周学习了Orm框架,那么Orm是什么呢!
对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换 。从效果上说,它其实是创建了一个可在编程语言里使用的--"虚拟对象数据库"。
面向对象是从软件工程基本原则(如耦合、聚合、封装)的基础上发展起来的,而关系数据库则是从数学理论发展而来的,两套理论存在显著的区别。为了解决这个不匹配的现象,对象关系映射技术应运而生。
对象关系映射(Object-Relational Mapping)提供了概念性的、易于理解的模型化数据的方法。ORM方法论基于三个核心原则: 简单:以最基本的形式建模数据。 传达性:数据库结构被任何人都能理解的语言文档化。 精确性:基于数据模型创建正确标准化的结构。 典型地,建模者通过收集来自那些熟悉应用程序但不熟练的数据建模者的人的信息开发信息模型。建模者必须能够用非技术企业专家可以理解的术语在概念层次上与数据结构进行通讯。建模者也必须能以简单的单元分析信息,对样本数据进行处理。ORM专门被设计为改进这种联系。
以上就是Orm的概念介绍,通过我的学习与理解,我们在数据库中的可以看到很多种类不同的表格,通过orm将表格设计映射成一个对象(C#中就是一个Class)
两个形成映射,我们在开发的时候都很不喜欢去连接数据库,将SQL写在一个字符串中,然后调用sql的excute去执行sql代码,一来sql语句很容易出错,二来还要通过不断调试测试来检验代码的准确性,三来当表格发生变化时不断调试再执行一二步骤,总之很麻烦,不方便去维护。
Orm就提供了关系对象的映射关系,我们通过操作Class来实现数据库的更新、删除、查询、修改等操作。
EntityFrame就是基于Orm的框架,而且还支持Oracle、SQLSEVER、MySQL等数据库。例如昨天用的Oracle,今天公司临时通知买不起Oracle了要改MySQL了,EntityFrame只需要修改不多的代码就能完成。
EntityFrame常见的有DataBase First 和 Code First 两种形式:
DataBase First:数据库提前创建好的情况下生成对应的Class对象(包括表格之间的关系)。
Code First :先创建好表格Class对象生成数据库表格。
Code FIrst(学生、老师、课程、成绩):
Course:
public Course()
{
this.students = new HashSet<Student>();
}
[Key]
[Column(Order =0)]
[DisplayName("课程号")]
public int CoID { get; set; }
[Column(Order =1)]
[DisplayName("课名")]
public string CoName { get; set; }
public virtual ICollection<Student> students { get; set; }
public virtual Teacher teacher { get; set; }
}
Student:
public class Student
{
public Student()
{
Courses = new HashSet<Course>();
}
[Key]
[Column(Order=0)]
[DisplayName("学号")]
public int StID { get; set; }
[Column(Order = 1)]
[DisplayName("姓名")]
public string StName { get; set; }
[Column(Order = 2)]
[DisplayName("联系电话")]
public string StNumber { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
Teacher:
public class Teacher
{
public Teacher()
{
Courses = new HashSet<Course>();
}
[Key]
[Column(Order=0)]
[DisplayName("教工号")]
public int TeID { get; set; }
[Column(Order=1)]
[DisplayName("教师姓名")]
public string TeName { get; set; }
[Column(Order=2)]
[DisplayName("教师职称")]
public string TePosition { get; set; }
[Column(Order=3)]
[DisplayName("联系方式")]
public string TeNumber { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
Score:
public class Score
{
[Key]
[ForeignKey("student")]
[Column(Order = 0)]
[DisplayName("学号")]
public int St { get; set; }
[Key]
[ForeignKey("course")]
[Column(Order = 1)]
[DisplayName("课程号")]
public int Co { get; set; }
[Column(Order =2)]
[DisplayName("得分")]
public int Scores { get; set; }
public Student student { get; set; }
public Course course { get; set; }
}
表格创建:
public class ManageEntity:DbContext
{
public ManageEntity() : base("name=Train")
{ }
public DbSet<Student> student { get; set; }
public DbSet<Course> course { get; set; }
public DbSet<Teacher> teacher { get; set; }
public DbSet<Score> score { get; set; }
/// <summary>
/// 创建时执行的函数
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasMany(x => x.Courses).WithMany(x=>x.students);
modelBuilder.Entity<Course>().HasRequired(x => x.teacher).WithMany(x => x.Courses);
base.OnModelCreating(modelBuilder);
}
}
这样就可以创建表格了(包括设计的表格之间的关系)
调用使用(添加数据):
ManageEntity studentDbcontext = new ManageEntity();
Teacher teacher1 = new Teacher()
{
TeID = 201901,
TeName = "ZS"
};
Teacher teacher2 = new Teacher()
{
TeID = 201902,
TeName = "WMZ"
};
Teacher teacher3 = new Teacher()
{
TeID = 201903,
TeName = "LS"
};
studentDbcontext.teacher.Add(teacher1);
studentDbcontext.teacher.Add(teacher2);
studentDbcontext.teacher.Add(teacher3);
Course course1 = new Course()
{
CoID = 1,
CoName = "英语",
teacher = teacher1
};
Course course2 = new Course()
{
CoID = 2,
CoName = "数学",
teacher = teacher2
};
Course course3 = new Course()
{
CoID = 3,
CoName = "语文",
teacher = teacher3
};
studentDbcontext.course.Add(course1);
studentDbcontext.course.Add(course2);
studentDbcontext.course.Add(course3);
Student student1 = new Student()
{
StID = 410,
StName = "PR",
Courses = new HashSet<Course>() { course1, course2 }
};
Student student2 = new Student()
{
StID = 445,
StName = "ZY",
Courses = new HashSet<Course>() { course1, course2, course3 }
};
Student student3 = new Student()
{
StID = 406,
StName = "LHY",
Courses = new HashSet<Course>() { course1 }
};
studentDbcontext.student.Add(student1);
studentDbcontext.student.Add(student2);
studentDbcontext.student.Add(student3);
studentDbcontext.student.Add(new Student()
{
StID = 445,
StName = "LRJ",
Courses = new HashSet<Course>() { studentDbcontext.course.FirstOrDefault(x => x.ID == "1") }
});
foreach (var st in studentDbcontext.student.ToList())
{
foreach (var co in studentDbcontext.course.ToList())
{
studentDbcontext.score.Add(new Score()
{
St = st.StID,
Co = co.CoID,
Scores = 90,
student=st,
course=co
});
}
}
studentDbcontext.SaveChanges();
查询:
ManageEntity studentDbcontext = new ManageEntity();
var tes = studentDbcontext.student;
foreach (var te in tes.ToList())
{
Console.WriteLine(te.StName);
}
Console.Read();