如何使用 EntityFramework CodeFirstStoreFunctions nuget 包?
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);
}
这与他们在指南中显示的不完全相同,但我在数据库中有一个函数,他们在代码中创建了它,所以我认为我不需要其余代码。
我有两个问题
1) 无论我试图从数据库中获取什么数据,我都会得到:
The argument 'name' cannot be null, empty or contain only white space.
2)
当我删除这部分代码时:
modelBuilder.Conventions.Add(new FunctionsConvention<TransitContext>("dbo"));
我明白了:
ErrorDescription = "'dbo.tvf_GetAllStopsInBetween' cannot be resolved
into a valid type or function."
Message = "'dbo.tvf_GetAllStopsInBetween' cannot be resolved into a
valid type or function. Near escaped identifier, line 1, column 1."
我是 codefirst 的新手,我继承了这段代码。
我该如何解决这个问题?
创建了一个 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);
}
我也得到了
The argument 'name' cannot be null, empty or contain only white space.
使用 nuget 包时。
我克隆了 codeplex 存储库并直接使用了源代码,那个错误就消失了。
我认为 nuget 实际上已经过时了
我正在尝试访问我的数据库中已经存在的函数。 我想先从代码执行这个函数。
我在网上搜索发现:
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);
}
这与他们在指南中显示的不完全相同,但我在数据库中有一个函数,他们在代码中创建了它,所以我认为我不需要其余代码。
我有两个问题
1) 无论我试图从数据库中获取什么数据,我都会得到:
The argument 'name' cannot be null, empty or contain only white space.
2) 当我删除这部分代码时:
modelBuilder.Conventions.Add(new FunctionsConvention<TransitContext>("dbo"));
我明白了:
ErrorDescription = "'dbo.tvf_GetAllStopsInBetween' cannot be resolved into a valid type or function."
Message = "'dbo.tvf_GetAllStopsInBetween' cannot be resolved into a valid type or function. Near escaped identifier, line 1, column 1."
我是 codefirst 的新手,我继承了这段代码。
我该如何解决这个问题?
创建了一个 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);
}
我也得到了
The argument 'name' cannot be null, empty or contain only white space.
使用 nuget 包时。
我克隆了 codeplex 存储库并直接使用了源代码,那个错误就消失了。 我认为 nuget 实际上已经过时了