[英]How to use EntityFramework CodeFirstStoreFunctions nuget package?
我正在嘗試訪問數據庫中已經存在的函數。 我想先從代碼執行此功能。
搜索我發現的網絡:
https://codefirstfunctions.codeplex.com/
我的實現:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TrainMessage>()
.Property(e => e.Latitude)
.HasPrecision(18, 16);
modelBuilder.Entity<TrainMessage>()
.Property(e => e.Longitude)
.HasPrecision(18, 16);
modelBuilder.Entity<Stop>()
.Property(e => e.stop_lat)
.HasPrecision(18, 16);
modelBuilder.Entity<Stop>()
.Property(e => e.stop_lon)
.HasPrecision(18, 16);
modelBuilder.Entity<Order>()
.Property(e => e.Latitude)
.HasPrecision(18, 16);
modelBuilder.Entity<Order>()
.Property(e => e.Longitude)
.HasPrecision(18, 16);
modelBuilder.Entity<Subscription>()
.Property(e => e.StartTime)
.HasPrecision(0);
modelBuilder.Entity<Subscription>()
.Property(e => e.EndTime)
.HasPrecision(0);
modelBuilder.Entity<OrderLocation>()
.Property(e => e.Latitude)
.HasPrecision(18, 16);
modelBuilder.Entity<OrderLocation>()
.Property(e => e.Longitude)
.HasPrecision(18, 16);
modelBuilder.Entity<Order>()
.HasMany(e => e.OrderLocations)
.WithOptional(e => e.Order)
.HasForeignKey(e => e.Order_Id);
modelBuilder.Conventions.Add(new FunctionsConvention<TransitContext>("dbo"));
}
[DbFunction("TransitContext", "tvf_GetAllStopsInBetween")]
public IQueryable<int> GetAllStopsBetweenStations(int StopA_id, int StopB_id)
{
var A = new ObjectParameter("StopA_id", StopA_id);
var B = new ObjectParameter("StopB_id", StopB_id);
return ((IObjectContextAdapter)this).ObjectContext
.CreateQuery<int>("[dbo.tvf_GetAllStopsInBetween](@StopA_id, StopB_id)", A, B);
}
它們與指南中所顯示的不完全相同,但是我在db中有一個函數,它們在代碼中創建了它,因此我認為我不需要其余的代碼。
我有兩個問題
1)無論我試圖從數據庫中獲取什么數據,我都會得到:
參數“名稱”不能為null,為空或僅包含空格。
2)當我刪除這部分代碼時:
modelBuilder.Conventions.Add(new FunctionsConvention<TransitContext>("dbo"));
我得到這個:
“ ErrorDescription =”'dbo.tvf_GetAllStopsInBetween'無法解析為有效的類型或函數。
Message =“'dbo.tvf_GetAllStopsInBetween'無法解析為有效的類型或函數。在轉義的標識符附近,第1行,第1列。”
我對Codefirst相當陌生,我繼承了此代碼。
我該如何解決?
我也有
參數“名稱”不能為null,為空或僅包含空格。
使用nuget包時。
我克隆了Codeplex存儲庫並直接使用源,該錯誤消失了。 我認為nuget實際上已經過時了
創建了一個SP而不是TVF,並使用了以下代碼,問題得以解決:
public ObjectResult<int> GetAllStopsBetweenStations(int StopA_id, int StopB_id)
{
var A = new SqlParameter("StopA_id", System.Data.SqlDbType.Int);
var B = new SqlParameter("StopB_id", System.Data.SqlDbType.Int);
A.Value = StopA_id;
B.Value = StopB_id;
return ((IObjectContextAdapter)this).ObjectContext.
ExecuteStoreQuery<int>("GetAllStopsInBetweenTest @StopA_id, @StopB_id ", A,B);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.