none
Entity Framework and Desktop application RRS feed

  • Question

  • Can you add Entity Framework to an existing Visual Basic Desktop application solution connecting to an SQL database.  If so, how would I go about doing it?  I see a lot of stuff applying to web applications but haven't really found anything on desktop applications.  Any help will be appreciated.
    Wednesday, June 24, 2020 9:30 PM

Answers

  • Hi mrbill65,

    If you want to create an Entity Framework model for an existed table, here are the steps you can refer to.

    First, right click the project and select "Add" => "New Item".

    Click "Data" tag, select "ADO.NET Entity Data Model" and type in the model name.

    Select "EF Designer from database".

    Click "New Connection", then input your "Server name" and select a database.

    Set the Entity name.

    The last step, select the table you want.(In my test, I selected table "tbStu")

    After all these steps finished, it will generate a "edmx" file.

    Then you can add data to the table "tbStu" via the following code.

        Dim mt As TestEntities = New TestEntities
        Dim stu As tbStu = New tbStu
        stu.Id = 1
        stu.Name = "Kyle"
        ' Insert the instance into database
        mt.tbStus.Add(stu)
        mt.SaveChanges()

    Update:

        Dim mt As TestEntities = New TestEntities
        Dim stu As tbStu = New tbStu
        stu.Id = 1
        stu.Name = "New name"
        ' Attach the entity need modified
        mt.tbStus.Attach(stu)
        ' Modify execute status
        mt.Entry(stu).State = System.Data.Entity.EntityState.Modified
        mt.SaveChanges

    Query:

        Dim mt As TestEntities = New TestEntities
        Dim query As Object = (From c In mt.tbStus
                                Where c.Id = 1
                                Select c).FirstOrDefault()
        Console.WriteLine(query.Name)

    Delete:

        Dim mt As TestEntities = New TestEntities
        Dim stu As tbStu = New tbStu
        stu.Id = 1
        mt.tbStus.Attach(stu)
        mt.Entry(stu).State = System.Data.Entity.EntityState.Deleted
        mt.SaveChanges()

    If you want to use "Code First", please refer to Code First to a New Database.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mrbill65 Monday, June 29, 2020 8:41 PM
    Thursday, June 25, 2020 3:38 AM
    Moderator
  • Hello,

    I have several articles on working with Entity Framework 6 that are all for Windows forms, VB.NET using code first with existing database. 

    VB.NET Entity Framework Wiki portal

    • I would suggest starting with this article.
    • Each of the articles in the link above have source code on GitHub listed at the bottom of each article
    • All articles have scripts to generate databases and tables with data.
    • None of the articles are complete solutions but instead focus on a specific topic.
    • Code first with existing database provides more flexibility than working with a designer (.edmx) 
    • There will be a mindset shift from DataSet/DataTable to strong typed classes but once learned you will not want to use DataSet/DataTable again.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by mrbill65 Monday, June 29, 2020 8:41 PM
    Monday, June 29, 2020 10:42 AM

All replies

  • Hi mrbill65,

    If you want to create an Entity Framework model for an existed table, here are the steps you can refer to.

    First, right click the project and select "Add" => "New Item".

    Click "Data" tag, select "ADO.NET Entity Data Model" and type in the model name.

    Select "EF Designer from database".

    Click "New Connection", then input your "Server name" and select a database.

    Set the Entity name.

    The last step, select the table you want.(In my test, I selected table "tbStu")

    After all these steps finished, it will generate a "edmx" file.

    Then you can add data to the table "tbStu" via the following code.

        Dim mt As TestEntities = New TestEntities
        Dim stu As tbStu = New tbStu
        stu.Id = 1
        stu.Name = "Kyle"
        ' Insert the instance into database
        mt.tbStus.Add(stu)
        mt.SaveChanges()

    Update:

        Dim mt As TestEntities = New TestEntities
        Dim stu As tbStu = New tbStu
        stu.Id = 1
        stu.Name = "New name"
        ' Attach the entity need modified
        mt.tbStus.Attach(stu)
        ' Modify execute status
        mt.Entry(stu).State = System.Data.Entity.EntityState.Modified
        mt.SaveChanges

    Query:

        Dim mt As TestEntities = New TestEntities
        Dim query As Object = (From c In mt.tbStus
                                Where c.Id = 1
                                Select c).FirstOrDefault()
        Console.WriteLine(query.Name)

    Delete:

        Dim mt As TestEntities = New TestEntities
        Dim stu As tbStu = New tbStu
        stu.Id = 1
        mt.tbStus.Attach(stu)
        mt.Entry(stu).State = System.Data.Entity.EntityState.Deleted
        mt.SaveChanges()

    If you want to use "Code First", please refer to Code First to a New Database.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mrbill65 Monday, June 29, 2020 8:41 PM
    Thursday, June 25, 2020 3:38 AM
    Moderator
  • Hello,

    I have several articles on working with Entity Framework 6 that are all for Windows forms, VB.NET using code first with existing database. 

    VB.NET Entity Framework Wiki portal

    • I would suggest starting with this article.
    • Each of the articles in the link above have source code on GitHub listed at the bottom of each article
    • All articles have scripts to generate databases and tables with data.
    • None of the articles are complete solutions but instead focus on a specific topic.
    • Code first with existing database provides more flexibility than working with a designer (.edmx) 
    • There will be a mindset shift from DataSet/DataTable to strong typed classes but once learned you will not want to use DataSet/DataTable again.


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by mrbill65 Monday, June 29, 2020 8:41 PM
    Monday, June 29, 2020 10:42 AM
  • Thank you.
    Monday, June 29, 2020 8:41 PM
  • Thank you.
    Monday, June 29, 2020 8:41 PM