locked
Entity Framework RRS feed

  • Question

  • Hi good people. where can find a complete step by step downloadable tutorial on Entity Framework in VB.NET


    John Sot

    Sunday, January 15, 2017 12:28 AM

Answers

  • There are no complete step by step guides for Entity Framework online or downloadable. 

    There are two camps for Entity Framework, Code First or Model First which variations to both. Then there are versions to contend with e.g. the current flavor is Entity Framework 6 which is being eclipsed by Entity Framework Core.

    What I can provide is as follows in regards to Model First (which is a personal choice made by a developer and easier to explain). In the following I'm using one project but generally you would break out EF to a class project.

    Create a new windows form project, add new item, under data select ADO.NET Entity Data Model, follow the prompts, from selecting a database table(s) to completion. At one point you will be prompted to provide an name for both the model and entity. Let's say I call my Entity DemoEntities and include a Customer table, no relations to other tables. We get the following (note Navigation properties, if there were relations they are listed here).

    To read data we create an instance of the entity and use LINQ or Lambda to read the data. If I wanted all Customers.

    Using entity As New DemoEntities
        DataGridView1.DataSource = entity.Customers.ToList
    End Using

    Sadly the DataGridView is not sortable so we need to use a custom component.

    Using entity As New DemoEntities
        DataGridView1.DataSource = New SortableBindingList(Of Customer)(entity.Customers.ToList)
    End Using

    In short we also want a BindingSource

    Using entity As New DemoEntities
        bsCustomers.DataSource = New SortableBindingList(Of Customer)(entity.Customers.ToList)
        DataGridView1.DataSource = bsCustomers
    End Using

    The BindingSource allows us to get the current item in the DataGridView

    Dim LastName As String = CType(bsCustomers.Current, Customer).LastName
    Dim Identifier As Integer = CType(bsCustomers.Current, Customer).id
    MessageBox.Show($"id: {Identifier} Lastname: {LastName}")

    Let's look at an edit for the current row in the DataGridView, we get the current value by casting, set the value, add the item to the context and set it's state follow by saving changes.

    Dim customer = CType(bsCustomers.Current, Customer)
    customer.FirstName = CType(bsCustomers.Current, Customer).FirstName
    Using entity As New DemoEntities
        entity.Entry(customer).State = Data.Entity.EntityState.Modified
        entity.SaveChanges()
    End Using

    Similarly an add is pretty much the same, delete is slightly different.

    I've touch very little here other then the basics. What I have not covered which is huge is validation that is not at form level but instead in the backend and my only public example is in C# on MSDN code sample,

    It's a walk in the park for the simple things but can be difficult to grasp when you get into more complex operations, no different then using a managed data provider but I prefer Entity Framework any day.

    In closing I recommend a subscription to Plural site and look at the following starting from the top if you want to go code first, if model first then you should still look at her stuff because there is nothing per-say on Model First out there.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, January 15, 2017 1:11 AM

All replies

  • I doubt you can. Most seem to be in C# and without knowing Entity Framework and reading the tutorials I could not recommend them anyhow in any language as I would not know how good they are. Nor do many of them seem to be downloadable as a .PDF or Word document from what I see.

    Perhaps you should ask at StackOverflow where you will need to become a member unless StackOverflow allows MSDN member logons which I doubt. The MSDN Forum for Entity Framework is no longer active and possibly archived.

    Also see Entity Framework (EF) Documentation.


    La vida loca

    Sunday, January 15, 2017 12:43 AM
  • There are no complete step by step guides for Entity Framework online or downloadable. 

    There are two camps for Entity Framework, Code First or Model First which variations to both. Then there are versions to contend with e.g. the current flavor is Entity Framework 6 which is being eclipsed by Entity Framework Core.

    What I can provide is as follows in regards to Model First (which is a personal choice made by a developer and easier to explain). In the following I'm using one project but generally you would break out EF to a class project.

    Create a new windows form project, add new item, under data select ADO.NET Entity Data Model, follow the prompts, from selecting a database table(s) to completion. At one point you will be prompted to provide an name for both the model and entity. Let's say I call my Entity DemoEntities and include a Customer table, no relations to other tables. We get the following (note Navigation properties, if there were relations they are listed here).

    To read data we create an instance of the entity and use LINQ or Lambda to read the data. If I wanted all Customers.

    Using entity As New DemoEntities
        DataGridView1.DataSource = entity.Customers.ToList
    End Using

    Sadly the DataGridView is not sortable so we need to use a custom component.

    Using entity As New DemoEntities
        DataGridView1.DataSource = New SortableBindingList(Of Customer)(entity.Customers.ToList)
    End Using

    In short we also want a BindingSource

    Using entity As New DemoEntities
        bsCustomers.DataSource = New SortableBindingList(Of Customer)(entity.Customers.ToList)
        DataGridView1.DataSource = bsCustomers
    End Using

    The BindingSource allows us to get the current item in the DataGridView

    Dim LastName As String = CType(bsCustomers.Current, Customer).LastName
    Dim Identifier As Integer = CType(bsCustomers.Current, Customer).id
    MessageBox.Show($"id: {Identifier} Lastname: {LastName}")

    Let's look at an edit for the current row in the DataGridView, we get the current value by casting, set the value, add the item to the context and set it's state follow by saving changes.

    Dim customer = CType(bsCustomers.Current, Customer)
    customer.FirstName = CType(bsCustomers.Current, Customer).FirstName
    Using entity As New DemoEntities
        entity.Entry(customer).State = Data.Entity.EntityState.Modified
        entity.SaveChanges()
    End Using

    Similarly an add is pretty much the same, delete is slightly different.

    I've touch very little here other then the basics. What I have not covered which is huge is validation that is not at form level but instead in the backend and my only public example is in C# on MSDN code sample,

    It's a walk in the park for the simple things but can be difficult to grasp when you get into more complex operations, no different then using a managed data provider but I prefer Entity Framework any day.

    In closing I recommend a subscription to Plural site and look at the following starting from the top if you want to go code first, if model first then you should still look at her stuff because there is nothing per-say on Model First out there.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, January 15, 2017 1:11 AM