Pergunta

My application has "Mainlines" which have many "Builds", which, in turn, have many "Releases". In VB.NET (C# is not allowed):

Partial Public Class Mainline
    Public Property MainlineID As Integer
    Public Property Designation As String
    ...
    Public Overridable Property Builds As ICollection(Of Build)
End Class

Partial Public Class Build
    Public Property BuildID As Integer
    Public Property Version As String
    ...
    Public Overridable Property Mainline As Mainline
    Public Overridable Property Releases As ICollection(Of Release)
End Class

Partial Public Class Release
    Public Property ReleaseID As Integer
    Public Property Version As Integer
    Public Property ReleaseDate As Date
    ...
    Public Overridable Property Build As Build
End Class

I've been doing Rails for many years now, so that's the terminology I know to talk about what I want, but I need a :has_many :through on Mainlines to get to Releases through Builds. I want to know how to get to this syntax:

db.Mainlines.Find(4).Releases

In terms of SQL, I want to do is this:

SELECT rels.*
FROM dbo.Releases rels,
dbo.Mainlines mls,
dbo.Builds blds
WHERE mls.MainlineID = blds.Mainline_MainlineID
AND blds.BuildID = rels.Build_BuildID
AND mls.MainlineID = 4
ORDER BY rels.ReleaseDate ASC

Specifically, I'm trying to look up all the "Releases" on a "Mainline" (which may have a different "Build") so that I can track what's changed (in models that are children of Releases) over time. (It would be nice to have a NuGet package for EF like an acts_like_tree gem for Rails, but tracking by latest previous date will get me close enough.)

It seems like there should be something that could be done, either in the class definition, or in the OnModelCreating section of the DbContext class, but I can't find anything, anywhere, that looks like an example of what I'm trying to do. It seems so basic, I'm pretty sure I'm just missing it.

There's a bunch of code at MSDN that shows Fluent API examples for stuff that I thought EF did automatically, which is confusing. (It makes me think that those docs are for EF4.) If you follow that example, I want:

SchoolEntities.Departments.First.Instructors
Foi útil?

Solução

Following should work (though I've not tried it)

db.Mainlines
    .Where(Function(m) m.MainlineID = 4)
    .SelectMany(Function(m) m.Builds.SelectMany(Function(b) b.Releases))
.ToList()

Originally, I wrote this in C# so giving the same for reference:

db.Mainlines
    .Where(m => m.MainlineID == 4)
    .SelectMany(m => m.Builds.SelectMany(b => b.Releases))
.ToList();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top