Linq to Entity-执行存储过程
来源:互联网 发布:java安装失败 卸载 编辑:程序博客网 时间:2024/06/12 07:05
1.在model中创建存储过程Model,将存储过程与实体进行映射,名称为存储过程名_Result:其中的属性是存储过程的返回值
create procedure "Ten Most Expensive Products" AS SET ROWCOUNT 10SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice FROM Products ORDER BY Products.UnitPrice DESC
public partial class Ten_Most_Expensive_Products_Result { public string TenMostExpensiveProducts { get; set; } public Nullable<decimal> UnitPrice { get; set; } }2.在DbContext子类中创建执行方法:传递存储过程名:Ten_Most_Expensive_Products
public virtual ObjectResult<Ten_Most_Expensive_Products_Result> Ten_Most_Expensive_Products() { return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Ten_Most_Expensive_Products_Result>("Ten_Most_Expensive_Products"); }3.测试执行存储过程:
static void TestProc() { using (NorthwindEntities NWEntities = new TestLINQToEntitiesApp.NorthwindEntities()) { IEnumerable<Ten_Most_Expensive_Products_Result> tenProducts = from p in NWEntities.Ten_Most_Expensive_Products() select p; foreach (Ten_Most_Expensive_Products_Result p in tenProducts) { Console.WriteLine("Product Name: {0}, Price: {1}", p.TenMostExpensiveProducts, p.UnitPrice); } } }4.如果存储过程返回的实体在model中已经存在了,则可以不用新建一个model, 直接利用原来的Model, 并直接在Dbcontext子类中创建执行方法:
Create PROCEDURE [dbo].[GetProduct] ( @ProductID int ) AS SET NOCOUNT ON Select * from Products where ProductID = @ProductID
public virtual ObjectResult<Product> GetProduct(Nullable<int> productID) { var productIDParameter = productID.HasValue ? new ObjectParameter("ProductID", productID) : new ObjectParameter("ProductID", typeof(int)); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Product>("GetProduct", productIDParameter); }
Product getProduct = NWEntities.GetProduct(1).FirstOrDefault();此时 由GetProduct得到的product是与Dbcontext关联的,对它进行任何操作都会执行数据库操作从而修改数据库的数据。
0 0
- Linq to Entity-执行存储过程
- LINQ TO Entity 执行存储过程(不能获得存储过程返回的0或1)
- entity 执行存储过程!
- linq to entity 调用存储过程(带返还结果集的存储过程)
- linq to sql 存储过程
- linq to sql 存储过程
- Entity Framework 执行sql语句 存储过程
- Linq to Entity CRUD
- LINQ TO SQL:动态执行带有有参数的存储过程
- linq to sql 应用存储过程
- sql to linq 之存储过程偏
- LINQ to SQL之存储过程
- Linq to Sql(五):存储过程
- LINQ to SQL语句之存储过程
- Linq to sql(五):存储过程
- Linq to sql(五):存储过程
- Linq to sql:普通存储过程
- LINQ to Entity 动态查询
- opencv︱图像的色彩空間cvtColor(HSV、HSL、HSB )及相关色彩学
- supermap desktop叠加天地图wmts服务偏差问题解决办法
- 从面试的角度诠释Java工程师
- Unable to determine local hostname -falling back to "localhost"
- 从HTTP和FTP上获取单个文件的大小
- Linq to Entity-执行存储过程
- POJ2534 Ubiquitous Religions
- HTTP报文-请求头部
- Linux下安装xgBoost
- Sprin mvc 出现 No mapping found for HTTP request with URI,解决方案之一
- 100道前端面试题系列01
- BigDecimal 加减乘除
- Spring Boot 学习12--普通类调用Bean
- MXNet:训练自己的数据并做预测