ORM ,不管是EF,hibernate,都是起到一个映射数据库对象的作用

在映射的基础上,提供很多功能和特性,比如,

数据库对象的class化访问

很多数据库本身没有提供的函数

直接使用编程框架提供的功能 

等等

不过我也有自己的思考,做为一个以前从事过长时间的数据库编程人员来说,总是感觉ORM麻烦,可能是人的习惯问题吧

需要一段时间的体验,才能知道优缺点,在这之前,还是抱着一定的怀疑态度  

 

EF代码的几种形式

  1. EntityClient+EntitySQL

    示例代码:

string city = "London";

using (EntityConnection cn = new EntityConnection("Name=Entities"))

{

cn.Open();

EntityCommand cmd = cn.CreateCommand();

cmd.CommandText = @"SELECT VALUE c FROM Entities.Customers AS c WHERE

c.Address.City = @city";

cmd.Parameters.AddWithValue("city", city);

DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

while (rdr.Read())

Console.WriteLine(rdr["CompanyName"].ToString());

rdr.Close();

}

 

  1. ObjectService+EntitySQL

在有EntityClient+EntitySQL这种使用方式下,使用ObjectService+EntitySQL的方式是多此一举,不会得到任何编辑时或运行时的好处。在ObjectContext下使用EntitySQL的真正作用是将其与LINQ to Entity结合使用。具体可见下文所示。

示例代码:

string city = "London";

using (Entities entities = new Entities())

{

ObjectQuery<Customers> query = entities.CreateQuery<Customers>(

"SELECT VALUE c FROM Customers AS c WHERE c.Address.City = @city",

new ObjectParameter("city", city)

);

 

foreach (Customers c in query)

Console.WriteLine(c.CompanyName);

}

 

  1. ObjectContext+LINQ( to Entity)

    方式一:

string city = "London";

using (Entities entities = new Entities())

{

var query = from c in entities.Customers

where c.Address.City == city

select c;

 

foreach (Customers c in query)

 Console.WriteLine(c.CompanyName);

}

    方式二:

string city = "London";

using (Entities entities = new Entities())

{

var query = entities.Customers.Where(r => r.Address.City == city);

 

foreach (Customers c in query)

 Console.WriteLine(c.CompanyName);

} 

 

如果一个应用决定了采用EF,我应该会大量的采用ObjectContext+LINQ( to Entity)

没有别的,至少看起来顺眼,其他方式的还不如直接用ADO.net

 

posted on 2010-09-26 09:17  YoungSin  阅读(3678)  评论(1编辑  收藏  举报