简体   繁体   English

对ef实体使用(linq?)

[英]Using (linq?) with ef entities

I have a fairly complex sql statement with joins. 我有一个带有连接的相当复杂的sql语句。 Here is my code using EF: 这是我使用EF的代码:

var MicrositeResponseAdors = _context.MicrositeResponseAdor
    .Include(user => user.UserTracking)
        .ThenInclude(tracking => tracking.AddTradeIn)
    .Include(user => user.UserTracking)
        .ThenInclude(tracking => tracking.TrackingFinance)
    .Include(user => user.UserTracking)
        .ThenInclude(tracking => tracking.SearchTracking)
    .Include(user => user.UserTracking)
        .ThenInclude(tracking => tracking.TrackingType)
    .Include(user => user.UserTracking)
        .ThenInclude(tracking => tracking.Vehicle)
    .FirstOrDefault(m => m.Id == id && m.FirstName.ToLower() != "test" && m.LastName.ToLower() != "test");

I need to add another join to a table that has no primary key, which Entity Framework doesn't allow. 我需要向没有主键的表中添加另一个联接,这是实体框架不允许的。 I wasn't even able to fake it. 我什至无法伪造它。 So I was told to make something more custom using what I believe is called LINQ? 因此,有人告诉我要使用我认为叫LINQ的东西做更多定制的事情?

I cannot figure out how to assign my data values with the original EF entity classes. 我无法弄清楚如何使用原始EF实体类分配数据值。 Here is what I have so far: 这是我到目前为止的内容:

var MicrositeResponseAdors = (from p in _context.MicrositeResponseAdor
    join c in _context.UserTracking on p.Id equals c.UserTrackingId
    into pc
    select new {
        MicrositeResponseAdor = p, MicrositeResponseAdor.UserTracking = pc 
    }
).FirstOrDefault();

This gives: 这给出:

Invalid anonymous type member declarator. 无效的匿名类型成员声明符。

I think that is because of the first part of MicrositeResponseAdor.UserTracking. 我认为这是因为MicrositeResponseAdor.UserTracking的第一部分。 I am not sure how to assign UserTracking under the MicrositeResponseAdor. 我不确定如何在MicrositeResponseAdor下分配UserTracking。

The error you've given is a C# compilation error, as you're trying to name a property MicrositeResponseAdor.UserTracking . 您正在尝试命名属性MicrositeResponseAdor.UserTracking ,给出的错误是C#编译错误。 Property names can't contain dots. 属性名称不能包含点。

This would at least compile: 这至少会编译:

var micrositeResponseAdor = (
    from p in _context.MicrositeResponseAdor
    join c in _context.UserTracking on p.Id equals c.UserTrackingId into pc
    select new {
        MicrositeResponseAdor = p,
        UserTracking = pc 
    }
).FirstOrDefault();

This will make your UserTracking appear in two places: 这将使您的UserTracking出现在两个位置:

  1. micrositeResponseAdor.UserTracking
  2. micrositeResponseAdor.MicrositeResponseAdor.UserTracking

It's in the first because you've put it there, and it's in the second because there's a relationship between the entities anyway, so EF is conveniently populating it if it has loaded it in that context (and it has, because you asked for it explicitly). 它在第一个中是因为您已将其放置在其中,而在第二个中则是因为实体之间仍然存在关系,因此,如果EF在该上下文中加载了它,那么EF会很方便地填充它(而且它是因为您要求它明确)。

So you don't have to assign loaded navigational properties in your query, you just have to make sure the related entities are loaded, either with a manual join, an Include or even a second query. 因此,您不必在查询中分配已加载的导航属性,只需确保通过手动联接, Include或什至第二次查询来加载相关实体。

The question is why you do this at all. 问题是为什么您要这样做。 You've already had the Include version. 您已经有了Include版本。 The join variant is just more verbose, but it doesn't add anything. join变体更为冗长,但不添加任何内容。

Not that EF will always have a key on every entity, whether the database thinks it is one or not. 并不是说EF总是在每个实体上都有一个密钥,无论数据库是否认为它是一个。 Technically, that's unrelated Include vs join though. 从技术上讲,尽管如此,但这并不Include vs join

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

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