如何使用 EF Code First 表示选项计算列?
我遇到的情况是,我有一个要查询的条目表,但在某些情况下,我会有其他可用信息。
例如,如果我有一张人员
表,我希望能够按姓名进行搜索,但我也想存储一些坐标并根据他们的位置进行搜索,同时还要在对象模型。因此,举例来说,假设我的 people
表看起来像这样:
PersonId int
Name nvarchar(100)
Address nvarchar(100)
Latitude float(10,6)
Longitude float(10,6)
实体类的定义如下:
public class Person
{
public int PersonId { get; set; }
public sting Name { get; set; }
public float Latitude { get; set; }
public float Longitude { get; set; }
}
然后我可以使用以下方法轻松按姓名查找人员:
var people = from p in myDb.People where p.Name.Contains("joe");
现在,我有一个名为的用户定义函数我创建的用于处理此距离计算的 CalculateDistance
。所以我的 SQL 看起来像这样:
String sql = "SELECT *, dbo.CalculateDistance(" + location.X + ", " + location.Y + ", Latitude, Longitude) AS Distance FROM people ORDER BY Distance
我如何在代码中表示它?我尝试将这样的属性添加到类中:
public virtual float Distance { get; set; }
但是名称查询失败,因为没有“距离”列。我还尝试扩展 Person 类:
public class PersonWithDistance: Person {
public float Distance { get; set; }
}
但这导致了映射生成方式的更多问题。
实施这样的事情的正确方法是什么?我是否必须为距离查询的结果创建一个完全独立的类?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个很好的设计。您应该将距离作为 Person 或某些辅助类的方法,并在应用程序中而不是在数据库中计算距离。它还需要将位置转移给该人。
无论如何,如果您想添加一些未映射到数据库中列的属性,则必须将其从映射中排除。 来完成:
这可以通过 attribute:或 Fluent API
This is not very nice design. You should have distance as a method of either Person or some helper class and compute distance in your application not in database. It whould also require transfering location to the person.
Anyway if you want to add some property which is not mapped to column in database you must exclude it from mapping. This can be done either by attribute:
or by fluent API:
好吧,我最终创建了一个
PersonResult
类,其中包含一个Id
、Distance
和一个 Person 对象,如下所示:我从存储过程填充它像这样:
并循环遍历结果来填充 Person 对象:
这似乎运作良好。虽然我不确定这是最好的处理方法。
Well I ended up creating a
PersonResult
class which contains anId
,Distance
and a Person object, like this:I populate that from a stored procedure like this:
And loop through the results to populate the Person objects:
That seems to work well. Although I'm not sure that's the best way to handle it.