.NET
Microsoft Technologies based on the .NET software framework.
3,383 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How to join two entities and retrieve columns from both entities as a new model list and use for each on new collection in VB.Net Entity Framework 6. any example will be helpful to learn
Hello,
Full source
https://github.com/karenpayneoregon/EntityFrameworkSolutionStructureWindowsForms_VisualBasic
Imports NorthWind.Data.Contexts
Namespace NorthWindOperations
Public Class Operations
Public Function CustomerJoined() As List(Of CustomerEntity)
Using context = New NorthWindContext()
Return (
From customer In context.Customers
Join contactType In context.ContactTypes On customer.ContactTypeIdentifier _
Equals contactType.ContactTypeIdentifier
Join contact In context.Contacts On customer.ContactIdentifier _
Equals contact.ContactIdentifier
Join country In context.Countries On customer.CountryIdentfier _
Equals country.Id
Select New CustomerEntity With
{
.CustomerIdentifier = customer.CustomerIdentifier,
.CompanyName = customer.CompanyName,
.ContactIdentifier = customer.ContactIdentifier,
.FirstName = contact.FirstName,
.LastName = contact.LastName,
.ContactTypeIdentifier = contactType.ContactTypeIdentifier,
.ContactTitle = contactType.ContactTitle,
.Address = customer.Street,
.City = customer.City,
.PostalCode = customer.PostalCode,
.CountryIdentifier = customer.CountryIdentfier,
.CountyName = country.CountryName}
).ToList()
End Using
End Function
End Class
End Namespace
Note for each table I include the primary key so if needed updates can be done to the database tables.
Namespace NorthWindOperations
Public Class CustomerEntity
Public Property CustomerIdentifier As Integer
Public Property CompanyName As String
Public Property ContactTitle As String
Public Property ContactIdentifier As Integer?
Public Property ContactTypeIdentifier As Integer
Public Property FirstName As String
Public Property LastName As String
Public Property PostalCode As String
Public Property CountryIdentifier As Integer?
Public Property CountyName As String
Public Property Address As String
Public Property City As String
Public Overrides Function ToString() As String
Return $"{CustomerIdentifier} {CompanyName}"
End Function
End Class
End NameSpace
If Entity Framework Core was being used see the following GitHub repository.