Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problems with Entity Famework and bindingsources

Problems with Entity Famework and bindingsources

Scheduled Pinned Locked Moved Visual Basic
databasecsharpcssquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    desanti
    wrote on last edited by
    #1

    Hello ! i'm using vb.net 2017 with EF 6 I have 2 tables on my database : Product and ProductDetail I have also 2 bindingsource Productbindingsource and ProductDetailsbindingsource. I have also 2 datagridview each of them bound to the respective bindingsource. When the user select a product in the first datagridview , the other datagridview is filled with Productdetails for the selected product. I have this code :

    Private sub Fill_Product
    context.Products.Where(Function(t2) t2.cl>5).Load
    ProductBindingsource.datasource=context.Products.Local.ToBindinglist
    End sub

    Private Sub ProductBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles ProductBindingSource.CurrentChanged

    context.Entry(ProductBindingSource.Current).Collection("ProductDetails").Load()
    ProductDetailsBindingsource.Datasource=DirectCast(ProductBindingSource.Current, Product).ProductDetails
    End sub

    Private Sub RollBack()
    Dim changedEntries = context.ChangeTracker.Entries().Where(Function(x) x.State <> EntityState.Unchanged).ToList()
    For Each entry In changedEntries

        Select Case entry.State
            Case EntityState.Modified
                entry.CurrentValues.SetValues(entry.OriginalValues)
                entry.State = EntityState.Unchanged
            Case EntityState.Added
                entry.State = EntityState.Detached
            Case EntityState.Deleted
                entry.State = EntityState.Unchanged
        End Select
    Next
    

    End Sub

    I have 2 problems with this code : 1.when I delete a ProductDetail from the second Grid , and after execute the rollback Sub , the deleted ProductDetail is not back on that datagrid , andalso when I add new Productdetail when I rollback this is not removed from the gridview.( other modifications in values , are reverted to original values). 2.On the sub ProductBindingSource_CurrentChanged , everytime that the user select a product in the first grid , the second grid is filled with ProductDetails and database is queried with .Load. But this is ok when the user select a Product for the first time . What about when the user select a Product for the second or third time ? The product Details are already loaded , so what to do to not query again from database but use local data ? Thank you !

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups