简体   繁体   English

EF 6.1.3和子资产

[英]EF 6.1.3 and child property

I have this mvc controller: 我有这个MVC控制器:

public ActionResult Index(string country)
{
    var c = _hc.Countries.Where(l => l.Code.Equals(country, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
    var customers = _hc.Customers.Where(h => h.Country.Code.Equals(country, StringComparison.InvariantCultureIgnoreCase));

    foreach (var h in customers)
        h.Country = c;

    return View(customers);
}

Everything works, but I just want the Customer.Country.Code to have a value. 一切正常,但我只希望Customer.Country.Code具有值。 And since this one lists for all customers with the same country... 由于此清单列出了同一国家/地区的所有客户...

Same for when I use a raw SqlQuery (because of the geolocation stuff) 与使用原始SqlQuery时相同(由于地理位置原因)

var customers = _hc.Database.SqlQuery<Customer>(@"
                SELECT customers.*, countries.code
                FROM [dbo].customers
                inner join countries on customers.countryid = countries.id
                where geography::Point(50.436912, 5.972050, 4326).STDistance(geography::Point([Latitude], [Longitude], 4326)) <= 25000").ToList();

I have the countryid in the customer object, because that is the Foreign Key. 我在客户对象中有countryid,因为那是外键。 And have seen some extension method which you could use to include other data. 并且已经看到了一些扩展方法,可以用来包含其他数据。 But according to my intellisens, it's not availble. 但是据我的才智,它不可用。 Probably something easy. 可能很容易。 But too hard for a friday afternoon (for me). 但是对于一个星期五的下午来说(对我来说)太难了。

In order to use the Include() extension method you need to add the following: 为了使用Include()扩展方法,您需要添加以下内容:

using System.Data.Entity;

See DbExtensions.Include 请参阅DbExtensions.Include

Example usage: 用法示例:

var customers = _hc.Customers
    .Include(h => h.Country)
    .Where(h => h.Country.Code.Equals(country, StringComparison.InvariantCultureIgnoreCase));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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