码迷,mamicode.com
首页 > 数据库 > 详细

Entity Framework Code First执行SQL语句、视图及存储过程

时间:2017-07-02 10:19:47      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:logs   pre   存储过程   输入参数   name   查询   etc   work   nat   

1、Entity Framework Code First查询表或视图

var provincelist = ctx.Provinces.SqlQuery("SELECT TOP 10 * FROM Province");
    foreach (var province in provincelist)
    {
        Console.WriteLine("{0}-{1}-{2}", province.ProvinceID, province.ProvinceNo, province.ProvinceName);
    }
    var provincelist = ctx.Database.SqlQuery<TempProvince>("SELECT TOP 10 ProvinceNo,ProvinceName FROM Province");
    foreach (var province in provincelist)
    {
        Console.WriteLine("{0}-{1}", province.ProvinceNo, province.ProvinceName);
    }

3、Entity Framework Code First执行存储过程

CREATE PROCEDURE GetCityByProvinceID
(
    @ProvinceID INT
)
AS
    SELECT * FROM City
    WHERE ProvinceID = @ProvinceID
using (var ctx = new PortalContext())
{
    var cityList = ctx.Cities.SqlQuery("dbo.GetCityByProvinceID @p0", 3);
    foreach (var city in cityList)
    {
        Console.WriteLine("{0}-{1}-{2}-{3}", city.CityID, city.ProvinceID, city.CityNo, city.CityName);
    }
}

存储过程多个输入参数:

var country = "Australia";
var keyWords = "Beach, Sun";
var destinations = context.Database.SqlQuery<DestinationSummary>("dbo.GetDestinationSummary @p0, @p1", country, keyWords);

 

Entity Framework Code First执行SQL语句、视图及存储过程

标签:logs   pre   存储过程   输入参数   name   查询   etc   work   nat   

原文地址:http://www.cnblogs.com/liandy0906/p/7104441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!