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









share|improve this question
























  • 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 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















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









share|improve this question
























  • 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 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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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 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
















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












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)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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
































    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)





    share|improve this answer

























      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)





      share|improve this answer























        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)





        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 4 at 10:59









        jmcilhinney

        24.4k21932




        24.4k21932






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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




















































































            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()