如何用EF Code First表示选项计算列? [英] How do I represent an option calculated column with EF Code First?

查看:707
本文介绍了如何用EF Code First表示选项计算列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况,我有一个我将要查询的条目表,但在某些情况下,我会提供其他信息。



例如,如果我有一个人的表,我希望能够通过名称进行搜索,但是我也想根据他们的位置存储一些坐标和搜索,但是在对象模型中暴露该距离。所以,例如,假设我的人员表格如下所示:

  PersonId int 
名称nvarchar(100)
地址nvarchar(100)
纬度float(10,6)
经度float(10,6)

实体类定义如下:

 code> public class Person 
{
public int PersonId {get;组; }
public sting Name {get;组; }
public float Latitude {get;组; }
public float Longitude {get;组; }
}

然后我可以通过名称轻松找到一个人:

  var people = from p in myDb.People where p.Name.Contains(joe); 

现在,我有一个名为 CalculateDistance 来处理这个距离计算。所以我的SQL将看起来像这样:

  String sql =SELECT *,dbo.CalculateDistance(+ location.X距离

我如何在代码中代表这个代码?我已经尝试向类添加这样的属性:

  public virtual float Distance {get; set;} 

但是,名称查询失败,因为没有Distance列,尝试扩展Person类:

  public class PersonWithDistance:Person {
public float Distance {get; set;}
}

但是,与生成映射的方式有更多的问题。 >

什么是正确的方式来实现这样的东西?我需要为距离查询的结果创建一个完全独立的单独类吗?

解决方案

我最终创建了一个 PersonResult 类,其中包含一个 Id Distance 和Person对象,如下所示:

  public class PersonResult {
[Key]
public int PersonId {get;组; }
public double Distance {get;组; }
public virtual Person Person {get;组;
}

我从这样的存储过程填充:

  var results = myDb.PersonResults.SqlQuery(EXEC PeopleByDistance+ lat +,+ lng); 

循环结果填充Person对象:

  foreach(结果中的PersonResultresult)
{
result.Person = myDb.People.Where(p => p.PersonId == result。 PERSONID).FirstOrDefault();
}

这似乎很好。虽然我不确定这是处理它的最好方法。


I have a situation where I have a table of entries that I'll be querying on, but in some cases I'll have additional information available.

For instance, if I have a table of people, I want to be able to search by name, but I also want to store some coordinates and search based on their location, but also expose that distance in the object model. So, for instance, suppose my people table looks something like this:

PersonId int
Name nvarchar(100)
Address nvarchar(100)
Latitude float(10,6)
Longitude float(10,6)

And the entity class is defined like this:

public class Person
{
     public int PersonId { get; set; }
     public sting Name { get; set; }
     public float Latitude { get; set; }
     public float Longitude { get; set; }
}

Then I can easily find a person by name using:

var people = from p in myDb.People where p.Name.Contains("joe");

Now, I have user-defined function called CalculateDistance that I've created to handle this distance calculations. And so my SQL will look something like this:

String sql = "SELECT *, dbo.CalculateDistance(" + location.X + ", " + location.Y + ", Latitude, Longitude) AS Distance FROM people ORDER BY Distance

How do I represent this in code? I've tried adding a property like this to the class:

public virtual float Distance { get; set; }

But then the name queries fail because there is no Distance column. I also tried extending the Person class:

public class PersonWithDistance: Person {
    public float Distance { get; set; }
}

But that caused even more problems with the way the mappings are generated.

What's the correct way to implement something like this? Do I have to create an entirely separate separate class for the results from the distance query?

解决方案

Well I ended up creating a PersonResult class which contains an Id, Distance and a Person object, like this:

public class PersonResult {
     [Key]
     public int PersonId { get; set; }
     public double Distance { get; set; }
     public virtual Person Person { get; set; }
}

I populate that from a stored procedure like this:

var results = myDb.PersonResults.SqlQuery("EXEC PeopleByDistance " + lat + ", " + lng);

And loop through the results to populate the Person objects:

foreach (PersonResultresult in results)
{
    result.Person = myDb.People.Where(p => p.PersonId == result.PersonId).FirstOrDefault();
}

That seems to work well. Although I'm not sure that's the best way to handle it.

这篇关于如何用EF Code First表示选项计算列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆