如何在Entity Framework函数导入中强制强制输入参数

时间:2013-09-18 11:01:06

标签: c# entity-framework stored-procedures

我正在使用数据库优先实体框架来访问现有数据库,并将其作为导入存储过程的一部分。问题是虽然存储过程中的输入参数不是可选的,但它们被映射为可为空。

这是存储过程声明:

CREATE PROCEDURE [dbo].[reverseGeocodeCity](
    @latitude FLOAT,
    @longitude FLOAT)
AS
--(snip)

函数导入映射将其创建为相应的方法:

public virtual ObjectResult<City> reverseGeocodeCity(
    Nullable<double> latitude, Nullable<double> longitude)

存储过程并不意味着接受NULL的任何参数。如何强制实体框架将输入参数映射为double而不是double?

1 个答案:

答案 0 :(得分:0)

我们遇到了同样的问题,我们找到了问题的解决方法。我们把双包裹起来?在我们的基础实体中加倍的财产; Hier是代码:

Notmapped实体(Wrapper实体)应从外部使用它,另一个实体将从DB中存储和加载。

继承的实体代码:

[NotMapped]
public double Longitude
{
  get
  {
    return this.FromNullable(this.longitude);
  }

  set
  {
    this.PropertyChange(ref this.longitude, value);
  }
}


[Column("Longitude")]
public double? LongitudeStored
{
  get
  {
    return this.longitude;
  }

  set
  {
    this.PropertyChange(ref this.longitude, value);
  }
}

Hier是基本实体代码:

protected double FromNullable(double? value)
{
  return value.HasValue ? value.Value : double.NaN;
}


protected void PropertyChange(ref double? propertyValue, double newValue, [CallerMemberName] string propertyName = "")
{
  this.PropertyChangeCore(ref propertyValue, double.IsNaN(newValue) ? (double?)null : (double?)newValue, propertyName);
}


protected void HandlePropertyCore(ref double? propertyValue, double? newValue, [CallerMemberName] string propertyName = "")
{
  if ((newValue.HasValue || propertyValue.HasValue) &&    // If both are null than do not fire.
       ((!propertyValue.HasValue || double.IsNaN(propertyValue.Value))
       ^ (!newValue.HasValue || double.IsNaN(newValue.Value))   // If one of them is null or NaN then fire according to XOr rule.
       || Math.Abs(propertyValue.Value - newValue.Value) > double.Epsilon)) // If the are not the same than fire.
  {
    propertyValue = newValue;
    this.HandlePropertyCore(propertyName); // HERE YOU NEED JUST TO HANDLE DOUBLE PROPERTY
  }
}