从图中可以看出,Entity Framework由许多逻辑层组成。最下面的层与实际的数据访问相关,涉及熟悉的ADO.NET数据提供程序。这不是提取数据的一种全新方法,而是对已有ADO.NET知识的扩展。其他层会使开发过程更容易、更灵活。在这一层上,Entity Framework与ADO.NET或LINQ to SQL没有区别,因为它直接处理表。
在实际的数据访问层的上面是存储层,这基本上表示数据库的结构,使用了XML语法。它包含添加到模型中的所有表,以及表之间的关系。
在存储层的上面是映射层。它用作在其下的存储层和其上的概念层之间的转换方式。可以把它看作一个相当薄的层,负责把数据库表的字段映射为应用程序使用的类的对应属性。
接着是概念层。它提供了模型的实体,以及在应用程序中使用的类——包括Entity Framework设计器生成的类和我们自己的类。
最后是对象服务层,用于在概念模型上提供LINQ或Entity Query Language(Entity SQL)语法。
因为Entity Framework使用核心的ADO.NET对象,所以使用这些类无法比ADO.NET更快或者与ADO.NET一样快,但是使用Entity Framework会得到自然得多的开发过程,创建和维护数据访问代码的效率也会高很多。
Module Module1
Private Sub testLinqToEntities1()
Dim db As New pubsEntities
Dim authors = From a In db.authors
Where a.state = "CA"
Order By a.au_lname, a.au_fname
Select a
For Each author In authors.ToList
Console.WriteLine("{0} {1}:{2}", author.au_fname, author.au_lname, author.phone)
Next
Console.WriteLine("Press ENTER to exit")
End Sub
Private Sub testLinqToEntities2()
Dim db As New pubsEntities
Dim newAuthor As authors = authors.Createauthors("555-55-5555", "deBar", "Foo", "555-555-1212", True)
db.authors.AddObject(newAuthor)
Dim authorKey As New EntityKey("pubsEntities.authors", "au_id", "527-72-3246")
Dim editAuthor As authors = CType(db.GetObjectByKey(authorKey), authors)
editAuthor.au_lname = "Green"
Console.WriteLine("Author state after edit:{0}", editAuthor.EntityState.ToString)
Dim recs As Integer = db.SaveChanges(Objects.SaveOptions.AcceptAllChangesAfterSave)
Console.WriteLine("{0} records changed", recs)
Console.WriteLine("Author state after save:{0}", editAuthor.EntityState.ToString)
Console.WriteLine("Press ENTER to exit")
End Sub
Sub Main()
'testLinqToEntities1()
testLinqToEntities2()
Console.Read()
End Sub
End Module