Reputation: 43
I have a simple method used to get a customer from the database by customerId:
Public Function GetCustomer(ByVal id As Long) As Customer
Using ctx As New MyEntities
Dim e = (From c In ctx.customers
Join bp In ctx.billingpoints
On c.customerId Equals bp.customerId
Select New Customer With {
.customerId = c.customerId
.billingPoint = New BillingPoint With{
.customerId = bp.customerId
.billingPointId = bp.billingPointId
'tons of more fields
}
'tons of more fields
})
Return e
End Using
End Function
This method returns a Customer object defined like this:
Public Class Customer
Public Property customerId As Long
Public Property billingPoint As BillingPoint
'many more fields
End Class
As you can see this looks horrible with all the properties being set. Is there any more effective way of doing this?
Upvotes: 0
Views: 71
Reputation: 10874
I assume you're asking because your object model does not contain an explicitly mapped relation property of type BillingPoint on the customer object, but only an implicit relationship between the two entities via customerId property on BillingPoint.
If you want to keep your model like this, which can be valid in many cases, you could consider mapping the query result to a CustomerDTO type. This can be done automatically using AutoMapper and its Queryable extensions:
https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions
Upvotes: 1
Reputation: 19101
Quick idea: Not sure if this will work but you might be able to add a Constructor in Customer
which takes the objects from your entities results as input parameters. Then you could do something like;:
Select New Customer(c, bc)
You might have to get the result using As Enumerable
to achieve this though, which means you will need to take care not to fetch and iterate over more results than you need to.
Upvotes: 0
Reputation: 372
You can use the classes generated already by Entity framework, without having the need of recreating your types with properties, unless you need a specific type of model, then you'll have to do the mapping manually, or write a generic extension that maps the columns to specific properties by "Name"
Upvotes: 0