Can I join two Entities and bind the result to a DataGridView in a way that the DataGridView edits can be...
up vote
1
down vote
favorite
I'm new to The Entity Framework. I need to show the result of two joined tables in a DataGridView and update the database when the user edited the data.
Without EF I usually fill a dataset and assign it to the DataSource property of a datatable and manually write the UpdateCommand. But don't know how to do this with EF.
This is how I'm implementing it right now:
Dim Query2 = From Product In db.Products
Join User In db.Users
On User.Id Equals Product.Owner.Id
Select New With {
Product.Id,
Product.Title,
User.UserName
}
DataGridView2.DataSource = Query2.ToList
vb.net entity-framework datagridview datatable jointable
add a comment |
up vote
1
down vote
favorite
I'm new to The Entity Framework. I need to show the result of two joined tables in a DataGridView and update the database when the user edited the data.
Without EF I usually fill a dataset and assign it to the DataSource property of a datatable and manually write the UpdateCommand. But don't know how to do this with EF.
This is how I'm implementing it right now:
Dim Query2 = From Product In db.Products
Join User In db.Users
On User.Id Equals Product.Owner.Id
Select New With {
Product.Id,
Product.Title,
User.UserName
}
DataGridView2.DataSource = Query2.ToList
vb.net entity-framework datagridview datatable jointable
You would have to give the "combined" type properties that passed-through to the properties of the original separate entities so you can't do it with an anonymous type unless you were to loop through the list of anonymous objects and update the original entities before saving.
– jmcilhinney
Nov 4 at 10:01
you'll have to have properties of your POCO entities bound to the view, and the properties of the navigation properties as well. Note that this will only work for updates, not inserts and deletes, and that this violates the SOLID principle.
– DevilSuichiro
Nov 4 at 10:04
@jmcilhinney Then I have to define an additional class for joined data, right? But what do you mean about passing through properties?
– rostamiani
Nov 4 at 10:46
1
If you want to be able to do what you said then yes, you need to define your own class. A "pass-through" property is one that, rather than storing a value in a private field of the current object, simply passes data through to and from another internal object. For instance, you might create a user control with aTextBox
as a child control and aTextBoxText
property. That property would simply get and set theText
propertyTextBox
, thus passing data through rather than storing it directly.
– jmcilhinney
Nov 4 at 10:49
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm new to The Entity Framework. I need to show the result of two joined tables in a DataGridView and update the database when the user edited the data.
Without EF I usually fill a dataset and assign it to the DataSource property of a datatable and manually write the UpdateCommand. But don't know how to do this with EF.
This is how I'm implementing it right now:
Dim Query2 = From Product In db.Products
Join User In db.Users
On User.Id Equals Product.Owner.Id
Select New With {
Product.Id,
Product.Title,
User.UserName
}
DataGridView2.DataSource = Query2.ToList
vb.net entity-framework datagridview datatable jointable
I'm new to The Entity Framework. I need to show the result of two joined tables in a DataGridView and update the database when the user edited the data.
Without EF I usually fill a dataset and assign it to the DataSource property of a datatable and manually write the UpdateCommand. But don't know how to do this with EF.
This is how I'm implementing it right now:
Dim Query2 = From Product In db.Products
Join User In db.Users
On User.Id Equals Product.Owner.Id
Select New With {
Product.Id,
Product.Title,
User.UserName
}
DataGridView2.DataSource = Query2.ToList
vb.net entity-framework datagridview datatable jointable
vb.net entity-framework datagridview datatable jointable
edited Nov 4 at 9:49
asked Nov 4 at 9:38
rostamiani
3791526
3791526
You would have to give the "combined" type properties that passed-through to the properties of the original separate entities so you can't do it with an anonymous type unless you were to loop through the list of anonymous objects and update the original entities before saving.
– jmcilhinney
Nov 4 at 10:01
you'll have to have properties of your POCO entities bound to the view, and the properties of the navigation properties as well. Note that this will only work for updates, not inserts and deletes, and that this violates the SOLID principle.
– DevilSuichiro
Nov 4 at 10:04
@jmcilhinney Then I have to define an additional class for joined data, right? But what do you mean about passing through properties?
– rostamiani
Nov 4 at 10:46
1
If you want to be able to do what you said then yes, you need to define your own class. A "pass-through" property is one that, rather than storing a value in a private field of the current object, simply passes data through to and from another internal object. For instance, you might create a user control with aTextBox
as a child control and aTextBoxText
property. That property would simply get and set theText
propertyTextBox
, thus passing data through rather than storing it directly.
– jmcilhinney
Nov 4 at 10:49
add a comment |
You would have to give the "combined" type properties that passed-through to the properties of the original separate entities so you can't do it with an anonymous type unless you were to loop through the list of anonymous objects and update the original entities before saving.
– jmcilhinney
Nov 4 at 10:01
you'll have to have properties of your POCO entities bound to the view, and the properties of the navigation properties as well. Note that this will only work for updates, not inserts and deletes, and that this violates the SOLID principle.
– DevilSuichiro
Nov 4 at 10:04
@jmcilhinney Then I have to define an additional class for joined data, right? But what do you mean about passing through properties?
– rostamiani
Nov 4 at 10:46
1
If you want to be able to do what you said then yes, you need to define your own class. A "pass-through" property is one that, rather than storing a value in a private field of the current object, simply passes data through to and from another internal object. For instance, you might create a user control with aTextBox
as a child control and aTextBoxText
property. That property would simply get and set theText
propertyTextBox
, thus passing data through rather than storing it directly.
– jmcilhinney
Nov 4 at 10:49
You would have to give the "combined" type properties that passed-through to the properties of the original separate entities so you can't do it with an anonymous type unless you were to loop through the list of anonymous objects and update the original entities before saving.
– jmcilhinney
Nov 4 at 10:01
You would have to give the "combined" type properties that passed-through to the properties of the original separate entities so you can't do it with an anonymous type unless you were to loop through the list of anonymous objects and update the original entities before saving.
– jmcilhinney
Nov 4 at 10:01
you'll have to have properties of your POCO entities bound to the view, and the properties of the navigation properties as well. Note that this will only work for updates, not inserts and deletes, and that this violates the SOLID principle.
– DevilSuichiro
Nov 4 at 10:04
you'll have to have properties of your POCO entities bound to the view, and the properties of the navigation properties as well. Note that this will only work for updates, not inserts and deletes, and that this violates the SOLID principle.
– DevilSuichiro
Nov 4 at 10:04
@jmcilhinney Then I have to define an additional class for joined data, right? But what do you mean about passing through properties?
– rostamiani
Nov 4 at 10:46
@jmcilhinney Then I have to define an additional class for joined data, right? But what do you mean about passing through properties?
– rostamiani
Nov 4 at 10:46
1
1
If you want to be able to do what you said then yes, you need to define your own class. A "pass-through" property is one that, rather than storing a value in a private field of the current object, simply passes data through to and from another internal object. For instance, you might create a user control with a
TextBox
as a child control and a TextBoxText
property. That property would simply get and set the Text
property TextBox
, thus passing data through rather than storing it directly.– jmcilhinney
Nov 4 at 10:49
If you want to be able to do what you said then yes, you need to define your own class. A "pass-through" property is one that, rather than storing a value in a private field of the current object, simply passes data through to and from another internal object. For instance, you might create a user control with a
TextBox
as a child control and a TextBoxText
property. That property would simply get and set the Text
property TextBox
, thus passing data through rather than storing it directly.– jmcilhinney
Nov 4 at 10:49
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Here's an example of the type of class I suggested:
Public Class UserProduct
Private user As User
Private product As Product
Public Property UserName As String
Get
Return user.UserName
End Get
Set
user.UserName = value
End Set
End Property
Public Property ProductId As Integer
Get
Return product.Id
End Get
Set
product.Id = value
End Set
End Property
Public Property ProductTitle As String
Get
Return product.Title
End Get
Set
product.Title = value
End Set
End Property
Public Sub New(user As User, product As Product)
Me.user = user
Me.product = product
End Sub
End Class
As you can see, the properties simply pass data through to the corresponding properties of the inner objects rather than storing it directly.
Your LINQ query would then become:
Select New UserProduct(user, product)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Here's an example of the type of class I suggested:
Public Class UserProduct
Private user As User
Private product As Product
Public Property UserName As String
Get
Return user.UserName
End Get
Set
user.UserName = value
End Set
End Property
Public Property ProductId As Integer
Get
Return product.Id
End Get
Set
product.Id = value
End Set
End Property
Public Property ProductTitle As String
Get
Return product.Title
End Get
Set
product.Title = value
End Set
End Property
Public Sub New(user As User, product As Product)
Me.user = user
Me.product = product
End Sub
End Class
As you can see, the properties simply pass data through to the corresponding properties of the inner objects rather than storing it directly.
Your LINQ query would then become:
Select New UserProduct(user, product)
add a comment |
up vote
1
down vote
Here's an example of the type of class I suggested:
Public Class UserProduct
Private user As User
Private product As Product
Public Property UserName As String
Get
Return user.UserName
End Get
Set
user.UserName = value
End Set
End Property
Public Property ProductId As Integer
Get
Return product.Id
End Get
Set
product.Id = value
End Set
End Property
Public Property ProductTitle As String
Get
Return product.Title
End Get
Set
product.Title = value
End Set
End Property
Public Sub New(user As User, product As Product)
Me.user = user
Me.product = product
End Sub
End Class
As you can see, the properties simply pass data through to the corresponding properties of the inner objects rather than storing it directly.
Your LINQ query would then become:
Select New UserProduct(user, product)
add a comment |
up vote
1
down vote
up vote
1
down vote
Here's an example of the type of class I suggested:
Public Class UserProduct
Private user As User
Private product As Product
Public Property UserName As String
Get
Return user.UserName
End Get
Set
user.UserName = value
End Set
End Property
Public Property ProductId As Integer
Get
Return product.Id
End Get
Set
product.Id = value
End Set
End Property
Public Property ProductTitle As String
Get
Return product.Title
End Get
Set
product.Title = value
End Set
End Property
Public Sub New(user As User, product As Product)
Me.user = user
Me.product = product
End Sub
End Class
As you can see, the properties simply pass data through to the corresponding properties of the inner objects rather than storing it directly.
Your LINQ query would then become:
Select New UserProduct(user, product)
Here's an example of the type of class I suggested:
Public Class UserProduct
Private user As User
Private product As Product
Public Property UserName As String
Get
Return user.UserName
End Get
Set
user.UserName = value
End Set
End Property
Public Property ProductId As Integer
Get
Return product.Id
End Get
Set
product.Id = value
End Set
End Property
Public Property ProductTitle As String
Get
Return product.Title
End Get
Set
product.Title = value
End Set
End Property
Public Sub New(user As User, product As Product)
Me.user = user
Me.product = product
End Sub
End Class
As you can see, the properties simply pass data through to the corresponding properties of the inner objects rather than storing it directly.
Your LINQ query would then become:
Select New UserProduct(user, product)
answered Nov 4 at 10:59
jmcilhinney
24.4k21932
24.4k21932
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139429%2fcan-i-join-two-entities-and-bind-the-result-to-a-datagridview-in-a-way-that-the%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You would have to give the "combined" type properties that passed-through to the properties of the original separate entities so you can't do it with an anonymous type unless you were to loop through the list of anonymous objects and update the original entities before saving.
– jmcilhinney
Nov 4 at 10:01
you'll have to have properties of your POCO entities bound to the view, and the properties of the navigation properties as well. Note that this will only work for updates, not inserts and deletes, and that this violates the SOLID principle.
– DevilSuichiro
Nov 4 at 10:04
@jmcilhinney Then I have to define an additional class for joined data, right? But what do you mean about passing through properties?
– rostamiani
Nov 4 at 10:46
1
If you want to be able to do what you said then yes, you need to define your own class. A "pass-through" property is one that, rather than storing a value in a private field of the current object, simply passes data through to and from another internal object. For instance, you might create a user control with a
TextBox
as a child control and aTextBoxText
property. That property would simply get and set theText
propertyTextBox
, thus passing data through rather than storing it directly.– jmcilhinney
Nov 4 at 10:49