ADO.NET Entity Framework,即下一代的ADO.NET。它是比Linq To SQL更加强大的ORM,让开发人员只需要着眼于领域对象模型的开发,而不需要考虑它们是如何与关系数据库交互。上一篇文章简单介绍了在项目中如何使用ADO.NET实体框架,从现在开始,正式进入了ADO.NET的学习之旅。这篇文章主要介绍在ADO.NET实体框架中如何进行查询(以Northwind数据库为例)。

1. 使用EntityCommand进行查询

   在实体框架中,我们可以通过EntityCommand进行查询,它的使用方法就像ADO.NET中的SqlCommand。不同的是SqlCommand使用标准SQL语句对数据库进行查询,而EntityCommand使用Entity SQL对EntityContainer进行查询,当然最终实体框架会将Entity SQL转换成标准SQL语句查询数据库。

  EntityConnection con = new EntityConnection("Name=NorthwindEntities");
  con.Open();

  using (EntityCommand cmd = 
      new EntityCommand("select value c from NorthwindEntities.Customers as c", con))
  {
      EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
      while (reader.Read())
      {
          Console.WriteLine("ID [{0}], ContactTitle [{1}]", 
              reader["CustomerID"], reader["ContactTitle"]);
      }
  }

  首先是建立一个EntityConnection,它接受一个参数,表明使用的是在config文件中的哪个连接字符串。

<connectionStrings>
   <add name="NorthwindEntities" connectionString="metadata=.\Northwind.csdl|.\Northwind.ssdl|.\Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost\sqlexpress;Initial Catalog=Northwind;Integrated Security=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

  可以看到这个连接字符串和以往ADO.NET中使用的连接字符串并不一样。metadata:指明.csdl/.ssdl/.msl三个文件的路径,这三个文件的作用以后再做说明。provider:表示使用的是SqlClient或者Oledb或者Odbc。provider connection string:这一项便是以往所用的连接字符串。providerName表示现在用的是EntityClient。

  接着构造EntityCommand,最后通过EntityDataReader进行数据的读取,值得注意的是这里的EntityCommand接受的是Entity SQL语句,而不是标准SQL语句,具体的Entity SQL语法可以参考帮助文档。

2. 使用ObjectQuery进行查询

  实体框架提供一个名为ObjectQuery的类,它让开发人员通过Entity SQL进行查询,查询结果以对象的形式供使用。

 using (NorthwindEntities ctx = new NorthwindEntities())
 {
     ObjectQuery<Customer> query
         = ctx.CreateQuery<Customer>("NorthwindEntities.Customers");
     ObjectResult<Customer> result = query.Execute(MergeOption.NoTracking);
     foreach (Customer c in result)
     {
         Console.WriteLine("ID [{0}], ContactTitle [{1}]",
          c.CustomerID, c.ContactTitle);
     }
 }

首先调用CreateQuery方法来创建ObjectQuery对象(当然这里也可以使用new,只是传进的参数不同而已),它接受Entity SQL语句作为参数。然后调用Execute方法进行查询,它接受MergeOption枚举型的参数,表示解决冲突的方法。查询结果以对象的形式(这里是Customer)保存在ObjectResult中。

下面是使用new的写法:

 using (NorthwindEntities ctx = new NorthwindEntities())
 {
     ObjectQuery<Customer> query = new ObjectQuery<Customer>("Customers", ctx);
     foreach (Customer c in query)
     {
         Console.WriteLine("ID [{0}], ContactTitle [{1}]",
          c.CustomerID, c.ContactTitle);
     }

 }

3. ADO.NET Entity Framework Tool自动生成Entities和各个对象的代码,帮助开发人员减少了很多体力活。这样,我们可以简单写成:

 using (NorthwindEntities ctx = new NorthwindEntities())
 {
     foreach (Customer c in ctx.Customers)
     {
         Console.WriteLine("ID [{0}], ContactTitle [{1}]",
          c.CustomerID, c.ContactTitle);

     }
 }

其实这里,也是使用ObjectQuery来进行查询。当然,可以给查询加上更多的条件,在上一篇文章中也有说明这里就不写了。

 

值得关注的是:自动生成的实体类中有这样一个方法,比如Customer:

 public static Customer CreateCustomer(string customerID, string companyName)
 {
     Customer customer = new Customer();
     customer.CustomerID = customerID;
     customer.CompanyName = companyName;
     return customer;
 }

并不像以前一样,提供带参数的构造函数,而是提供CreateCustomer的静态方法来构造Customer实例。这似乎和前一段时候贫血充血的问题有关了,实体对象该不该有行为,是贫血还是充血?虽然只是一个方法,不过相信这也表明了微软的态度吧。

 posted on 2007-10-14 05:36  紫色阴影  阅读(19721)  评论(17编辑  收藏  举报
我要啦免费统计