Entity Framework Code First執行SQL語句、視圖及存儲過程

一、Entity Framework Code First查詢視圖app

  Entity Framework Code First目前尚未特別針對View操做的方法,但對於可更新的視圖,能夠採用與Table同樣的方式進行插入、修改、刪除及查詢。在實際的項目過程當中,視圖多隻用於進行查詢。ide

  Entity Framework Code First查詢視圖示例:ui

  使用到的表及視圖結構以下:this

  文件類VCity.cs:spa

using System;
using System.Collections.Generic;

namespace Portal.Models
{
    public class VCity
    {
        public int CityID { get; set; }
        public Nullable<int> ProvinceID { get; set; }
        public string ProvinceNo { get; set; }
        public string ProvinceName { get; set; }
        public string CityNo { get; set; }
        public string CityName { get; set; }
    }
}
View Code

  映射文件類VCityMap.cs:3d

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace Portal.Models.Mapping
{
    public class VCityMap : EntityTypeConfiguration<VCity>
    {
        public VCityMap()
        {
            // Primary Key
            this.HasKey(t => t.CityID);

            // Properties
            this.Property(t => t.CityID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.ProvinceNo)
                .HasMaxLength(10);

            this.Property(t => t.ProvinceName)
                .HasMaxLength(50);

            this.Property(t => t.CityNo)
                .HasMaxLength(10);

            this.Property(t => t.CityName)
                .HasMaxLength(50);

            // Table & Column Mappings
            this.ToTable("VCity");
            this.Property(t => t.CityID).HasColumnName("CityID");
            this.Property(t => t.ProvinceID).HasColumnName("ProvinceID");
            this.Property(t => t.ProvinceNo).HasColumnName("ProvinceNo");
            this.Property(t => t.ProvinceName).HasColumnName("ProvinceName");
            this.Property(t => t.CityNo).HasColumnName("CityNo");
            this.Property(t => t.CityName).HasColumnName("CityName");
        }
    }
}
View Code

  文件類PortalContext.cs:code

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using Portal.Models.Mapping;

namespace Portal.Models
{
    public class PortalContext : DbContext
    {
        static PortalContext()
        {
            Database.SetInitializer<PortalContext>(null);
        }

        public PortalContext()
            : base("Name=PortalContext")
        {
        }

        public DbSet<City> Cities { get; set; }
        public DbSet<Province> Provinces { get; set; }
        public DbSet<VCity> VCities { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CityMap());
            modelBuilder.Configurations.Add(new ProvinceMap());
            modelBuilder.Configurations.Add(new VCityMap());
        }
    }
}
View Code

  文件類Program.cs,用於查詢視圖:blog

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Models;
namespace Portal
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new PortalContext())
            {
                foreach (var vCity in ctx.VCities)
                {
                    Console.WriteLine("{0}-{1}-{2}-{3}-{4}-{5}", vCity.CityID, vCity.ProvinceID, vCity.ProvinceNo, vCity.ProvinceName, vCity.CityNo, vCity.CityName);
                }
            }

            Console.ReadKey();
        }
    }
}
View Code

二、Entity Framework Code First執行SQL語句ci

  在使用Entity Framework Code First時,當須要直接執行SQL時,能夠使用SqlQuery方法。SqlQuery方法採用屬性名即列名的方法進行映射查詢,要求返回的查詢結果均有徹底對應的類屬性。get

  1>、已定義的表映射類查詢

using (var ctx = new PortalContext())
{
    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);
    }
}

  2>、未有定義表的臨時SQL語句查詢

  示例:須要查詢Province表中的ProvinceNo,ProvinceName,首先定義一個臨時類TempProvince.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Portal.Models
{
    public class TempProvince
    {
        public string ProvinceNo { get; set; }
        public string ProvinceName { get; set; }
    }
}
View Code

  執行SQL語句查詢:

using (var ctx = new PortalContext())
{
    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);
    }
}

三、Entity Framework Code First執行存儲過程

  Entity Framework Code First執行存儲過程一樣是使用SqlQuery方法。

  建立存儲過程:

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);
相關文章
相關標籤/搜索