none
Data Sources: Database vs. Object RRS feed

  • Question

  • I have added a SQL database as data source of type object to my project according to this tutorial: https://docs.microsoft.com/en-us/ef/ef6/fundamentals/databinding/winforms.

    It is also posssible to add a database as data source of type database to my project. What is the differenece between these 2 options? Are there differences in terms of performance? Is one of these 2 methods easier to use in development?


    Saturday, May 23, 2020 6:46 PM

All replies

  • Hi QuestionBot,
    According to my understanding, using the database as a datesoure is more direct and accurate. And you can query data from the database directly .
    When using an object as a data source, you can query the corresponding results through different attributes of the object. But the final operation is the data in the database.
    Best Regards,
    Daniel Zhang


    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.

    Monday, May 25, 2020 7:14 AM
  • Sounds like using an object provides a second layer with objects representing database entities. Is there any benefit using an object instead of a database or is it just a question of taste?
    Monday, May 25, 2020 10:31 PM
  • Hi QuestionBot,
    About data source for an object, all public properties of an object are displayed in the Data Sources window. If you are using Entity Framework and have generated a model, this is where you find the entity classes that are the data sources for your application.
    Here is a document about the kinds of data source you can refer to.
    Best Regards,
    Daniel Zhang


    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.

    Tuesday, May 26, 2020 7:26 AM
  • Hello,

    A better path to follow is Entity Framework Core 3 "Code First from database" either from first timers or experience developers. Using "Code First from database" usually means creating a database, tables and relationships in SSMS (SQL-Server Management Studio) where you get immediate feedback to the validity of your relationship by populating data and running SQL statements to ensure things work as expected. Once this is done with EF Core (Entity Framework Core) using the following Visual Studio extension reverse engineer your database to classes.

    The results from running the extension are shown in the red area.

    Next up, you need to decide to work with attached or detached models (classes representing tables).  

    In regards to sorting you can use the following class SortableBindingList. Then implement INotifyPropertyChanged to (if this is intent) and subscribe to ListChanged event of the BindingList (optional) for interrogating changes e.g.

    private void _productView_ListChanged(object sender, ListChangedEventArgs e)
    {
        if (e.ListChangedType == ListChangedType.ItemDeleted)
        {
    
            /*
             * Access deleted products
             */
            var deleted = Operations.Context.ChangeTracker.Entries()
                .Where(item => item.State == EntityState.Deleted)
                .Select(item => item.Entity as Products).ToList();
    
        }
    
        var currentProduct = _productView[_productBindingSource.Position];
    
        if (e.ListChangedType == ListChangedType.ItemChanged)
        {
            // TODO - anything needed say in validation
        }
        else if (e.ListChangedType == ListChangedType.ItemAdded)
        {
            // TODO - anything needed say in validation
        }
    }
    
    

    If you want to delete rows from the row header of a DataGridView you will need to set the state e.g.

    private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) => 
        Operations.Context.Entry(_productView[e.Row.Index]).State = EntityState.Deleted;
    

    This is done because it's easier than in ListChanged event.

    The above is a glimpse at working with Code First from database. 

    No matter which direction taken consider separating data operations from forms e.g. create a class for data operations as shown in this class where the code presented is demoing LINQ/Lambda Projections.

    Bottom line is to consider using EF Core and manually rigging up code rather than using the wizard.


    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

    Tuesday, May 26, 2020 10:48 AM
    Moderator
  • Hi Daniel,

    thanks for your replies. The link you shared (https://docs.microsoft.com/en-us/visualstudio/data-tools/add-new-data-sources?view=vs-2019) lists the different data source types but it doesn't really help me to decide what option is best for me. I found this document https://docs.microsoft.com/en-gb/visualstudio/data-tools/bind-controls-to-data-in-visual-studio?view=vs-2019 that compares compatibilities of different data source types.

    So, one difference between the two data source types "Database" and "Object" is that an Object supports drag-and-drop in the Silverlight Designer.

    Is this supposed to be the main difference between the two data source types?

    Tuesday, May 26, 2020 10:52 AM
  • Hi QuestionBot,
    I viewed the document you provided and data source type "Object" supports more and it is more convenient.
    The main difference is that data source type "Database" gets data directly from the database, it is more accurate.
    Best Regards,
    Daniel Zhang


    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.

    Wednesday, May 27, 2020 8:28 AM
  • Hi Karen,

    thanks for the detailed instructions. The ef-core-projections solution is a good introduction to learn about databound controls. Still I was hoping that there are easier methods to databind controls using wizards (e.g. like in ms access).

    I understand in .NET Core there are still many features not working as in .NET Framework. But even in .NET Framework databinding controls doesn't work only by using the controls' properties. Especially when saving data back to the database.


    Friday, June 5, 2020 9:38 PM
  • Hello,

    Please note that there will never be wizards like Microsoft Access to data bind for Windows Forms. 

    If you were working with WPF, also no wizards but WPF has much better methods to data bind (like two way binding). See the following Microsoft documentation. If interested see the following Microsoft TechNet article which doesn't use Entity Framework but would easily work via a DbContext that has change tracking enabled.

    Shown with VB.NET yet the same applies for C#

     


    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

    Monday, June 8, 2020 12:09 PM
    Moderator
  • Please note that there will never be wizards like Microsoft Access to data bind for Windows Forms.

    This is sad. All my nerdy dreams are broken into little pieces :)

    And as far as I know WPF will never support developing cross-plattform applications.


    Monday, June 15, 2020 6:52 PM
  • Please note that there will never be wizards like Microsoft Access to data bind for Windows Forms.

    This is sad. All my nerdy dreams are broken into little pieces :)

    And as far as I know WPF will never support developing cross-plattform applications.


    Nope, WPF will not support cross platform but Xamarin does.


    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

    Tuesday, June 16, 2020 12:35 PM
    Moderator