1、Entity Framework Code First查詢檢視
Entity Framework Code First目前還沒有特別針對View操作的方法,但對於可更新的檢視,可以採用與Table一樣的方式進行插入、修改、刪除及查詢。在實際的專案過程中,檢視多隻用於進行查詢。
Entity Framework Code First查詢檢視示例:
使用到的表及檢視結構如下:
檔案類VCity.cs:
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; } } }
對映檔案類VCityMap.cs:
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"); } } }
檔案類PortalContext.cs:
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()); } } }
檔案類Program.cs,用於查詢檢視:
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(); } } }
2、Entity Framework Code First執行SQL語句
在使用Entity Framework Code First時,當需要直接執行SQL時,可以使用SqlQuery方法。SqlQuery方法採用屬性名即列名的方法進行對映查詢,要求返回的查詢結果均有完全對應的類屬性。
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; } } }
執行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); } }
3、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);