简体   繁体   English

如何使用EntityFramework CodeFirstStoreFunctions nuget包?

[英]How to use EntityFramework CodeFirstStoreFunctions nuget package?

I am trying to access an already existing function that is in my db. 我正在尝试访问数据库中已经存在的函数。 I want to execute this function from code first. 我想先从代码执行此功能。

Searching the net i found: 搜索我发现的网络:

https://codefirstfunctions.codeplex.com/ https://codefirstfunctions.codeplex.com/

My implementation of this: 我的实现:

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);


}

It is not exactly what they show in the guide but I have a function in the db and they create it in the code so I figure I do not need the rest of the code. 它们与指南中所显示的不完全相同,但是我在db中有一个函数,它们在代码中创建了它,因此我认为我不需要其余的代码。

I have two problems with this 我有两个问题

1) No matter what data I am trying to get from the db I get: 1)无论我试图从数据库中获取什么数据,我都会得到:

The argument 'name' cannot be null, empty or contain only white space. 参数“名称”不能为null,为空或仅包含空格。

2) When I remove this part of the code: 2)当我删除这部分代码时:

modelBuilder.Conventions.Add(new FunctionsConvention<TransitContext>("dbo"));

I get this: 我得到这个:

ErrorDescription = "'dbo.tvf_GetAllStopsInBetween' cannot be resolved into a valid type or function." “ ErrorDescription =”'dbo.tvf_GetAllStopsInBetween'无法解析为有效的类型或函数。

Message = "'dbo.tvf_GetAllStopsInBetween' cannot be resolved into a valid type or function. Near escaped identifier, line 1, column 1." Message =“'dbo.tvf_GetAllStopsInBetween'无法解析为有效的类型或函数。在转义的标识符附近,第1行,第1列。”

I am fairly new to codefirst and I inherited this code. 我对Codefirst相当陌生,我继承了此代码。

How do i solve this? 我该如何解决?

I also got 我也有

The argument 'name' cannot be null, empty or contain only white space. 参数“名称”不能为null,为空或仅包含空格。

when using the nuget package. 使用nuget包时。

I cloned the codeplex repo and used the source directly and that error went away. 我克隆了Codeplex存储库并直接使用源,该错误消失了。 I think the nuget is actually out of date 我认为nuget实际上已经过时了

created a SP instead of a TVF and used the following code and problem was solved: 创建了一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM