添加EntityFramework类库,添加数据库连接,ADO.NET Entity Data Model,在Entity Data Model Wizard中选择一个BookContext(上次创建Book类的数据库)数据库连接,
示例代码如下:
- using (var db = new BookContext())
- {
- var query = from b in db.Books
- orderby b.Title
- select b;
- foreach (var s in query)
- {
- XElement bookElement = new XElement("book",
- new XElement("title",s.Title),
- new XElement("author",s.Author)
- );
- Console.WriteLine(bookElement);
- }
- Console.Write("Program finished, press Enter/Return to continue:");
- Console.ReadLine();
- }