如何使用 EF Code First 表示选项计算列?

发布于 2024-10-22 01:29:01 字数 1235 浏览 0 评论 0原文

我遇到的情况是,我有一个要查询的条目表,但在某些情况下,我会有其他可用信息。

例如,如果我有一张人员表,我希望能够按姓名进行搜索,但我也想存储一些坐标并根据他们的位置进行搜索,同时还要在对象模型。因此,举例来说,假设我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2024-10-29 01:29:01

这不是一个很好的设计。您应该将距离作为 Person 或某些辅助类的方法,并在应用程序中而不是在数据库中计算距离。它还需要将位置转移给该人。

无论如何,如果您想添加一些未映射到数据库中列的属性,则必须将其从映射中排除。 来完成:

[NotMapped]
public float Distance { get; set; }

这可以通过 attribute:或 Fluent API

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Person>().Ignore(p => p.Distance);
}

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:

[NotMapped]
public float Distance { get; set; }

or by fluent API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Person>().Ignore(p => p.Distance);
}
陌路终见情 2024-10-29 01:29:01

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

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

我从存储过程填充它像这样:

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

并循环遍历结果来填充 Person 对象:

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

这似乎运作良好。虽然我不确定这是最好的处理方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文