Entity Framework 6 Recipes 2nd Edition(10-1)译->非Code Frist方式返回一个实体集合
存储过程
存储过程一直存在于任何一种关系型数据库中,如微软的SQL Server.存储过程是包含在数据库中的一些代码,通常为数据执行一些操作,它能为数据密集型计算提高性能,也能执行一些为业务逻辑. 当你使用数据的时候,有时你会通过存储过程来获取它们.
在本章, 我们探讨一些EF在使用存储过程时,需要关注的地方。我们在本书的其它章节也使用了存储过程, 但通常都是context为执行插入、更新和删除动作。
在本章,我们将为你展示多种使用存储过程的方式。
10-1. 非Code Frist方式返回一个实体集合
问题
想用非Code Frist方式从存储过程里取得一个实体集合
解决方案
Code second (我把它译为非Code Frist)是参照 Code-First 技术,为一个已经存在的数据库建模的方式
我们假设有一个 POCO模型,如Listing 10-1所示:
Listing 10-1. The Customer POCO Model
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public string ContactTitle { get; set; }
}
我们已经设置好了DbContext子类和Customer 实体集,如Listing 10-2所示:
Listing 10-2. The DbContext Subclass for Customer Entities
public class EF6RecipesContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public EF6RecipesContext() : base("name=EF6CodeFirstRecipesContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Types<Customer>()
.Configure(c =>
{
c.HasKey(cust => cust.CustomerId);
c.Property(cust => cust.CustomerId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
c.Property(cust => cust.Name)
.HasMaxLength(50);
c.Property(cust => cust.Company)
.HasMaxLength(50);
c.Property(cust => cust.ContactTitle)
.HasMaxLength(50);
c.ToTable("Customer", "Chapter10");
});
}
}
在数据库中,我们已经定义了如Listing 10-3所示的存储过程,该存储过程根据公司名称和客户标题返回符合条件的 customer
Listing 10-3. GetCustomers Returns All of the Customers with the Given Title in the Given Company.
create procedure Chapter10.GetCustomers
(@Company varchar(50),@ContactTitle varchar(50))
as
begin
select * from
chapter10.Customer where
(@Company is null or Company = @Company) and
(@ContactTitle is null or ContactTitle = @ContactTitle)
End
为了在方法中使用 GetCustomers 存储过程,操作如下:
1. 在DbContext 子类中创建一个公开的方法(命名为GetCustomers),它接受两个string参数,并返回Customer集合, 如 Listing 10-4所示.
Listing 10-4. A New Method to Return a Collection of Customer Objects
public ICollection<Customer> GetCustomers(string company, string contactTitle)
{
throw new NotImplementedException();
}
2.接下来实现这个GetCustomers() 方法,它调用DbContext.Database的SqlQuery方法DbContext.Database
如Listing 10-5所示.
Listing 10-5. DbContext Subclass with GetCustomers() Implementation
public class EF6RecipesContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public EF6RecipesContext() : base("name=EF6CodeFirstRecipesContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Types<Customer>()
.Configure(c =>
{
c.HasKey(cust => cust.CustomerId);
c.Property(cust => cust.CustomerId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
c.Property(cust => cust.Name)
.HasMaxLength(50);
c.Property(cust => cust.Company)
.HasMaxLength(50);
c.Property(cust => cust.ContactTitle)
.HasMaxLength(50);
c.ToTable("Customer", "Chapter10");
});
}
public ICollection<Customer> GetCustomers(string company, string contactTitle)
{
return Database.SqlQuery<Customer>( "EXEC Chapter10.GetCustomers @Company,
@ContactTitle"
, new SqlParameter("Company", company)
, new SqlParameter("ContactTitle", contactTitle))
.ToList();
}
}
3.接下来的这个代码段Listing 10-6 就是调用GetCustomers存储过程.
Listing 10-6. Querying the Model with the GetCustomers Stored Procedure via the GetCustomers()
Method
//插入一些Customer,让存储过程查询.
using (var context = new EF6RecipesContext())
{
var c1 = new Customer {Name = "Robin Steele", Company = "GoShopNow.com",
ContactTitle="CEO"};
var c2 = new Customer {Name = "Orin Torrey", Company = "GoShopNow.com",
ContactTitle="Sales Manager"};
var c3 = new Customer {Name = "Robert Lancaster", Company = "GoShopNow.com",
ContactTitle = "Sales Manager"};
var c4 = new Customer { Name = "Julie Stevens", Company = "GoShopNow.com",
ContactTitle = "Sales Manager" };
context.Customers.Add(c1);
context.Customers.Add(c2);
context.Customers.Add(c3);
context.Customers.Add(c4);
context.SaveChanges();
}
using (var context = new EF6RecipesContext())
{
var allCustomers = context.GetCustomers("GoShopNow.com", "Sales Manager");
Console.WriteLine("Customers that are Sales Managers at GoShopNow.com");
foreach (var c in allCustomers)
{
Console.WriteLine("Customer: {0}", c.Name);
}
}
以下Listing 10-6是控制台输出结果:
============================================================================================
Customers that are Sales Managers at GoShopNow.com
Customer: Orin Torrey
Customer: Robert Lancaster
Customer: Julie Stevens
=============================================================
它是如何工作的?
为了能接收数据库中的存储过程里返回的实体集合,我们在DbContext子类中实现了 GetCustomers()方法,该方法用DbContext.Database.SqlQuery<T>() 来执行存储过程 GetCustomers(它的定义见Listing 10-3). SqlQuery() 方法能用来执行返回一个结果集的 DML(数据操纵语言)语句. 该方法接收一个SQL语句的字符串。SqlQuery<T>() 泛型方法返回一个开发人员指定的强类型的实体集。
kid1412声明:转载请把此段声明完整地置于文章页面明显处,并保留个人在博客园的链接:http://www.cnblogs.com/kid1412/(可点击跳转)。