ORM, DataBinding for DataGridView: insert / delete new rows not saved in the database

I am new to ORM and currently I offer Telerik OpenAccess ORM, but in fact the question may not be specific to this ORM, and I still have not completely decided to use this ORM. p>

What I'm trying to achieve is to bind a DataGridView to show a collection of Customers objects showing all the clients in the clients table.

I bound it to a BindingSource and bound a BindingSource to a DataGridView control.

I can successfully modify existing elements (using the SaveChanges method for OpenAccess ORM). When I save, the content is saved back to the database as I expected.

However, if I delete a row from the DataGridView or add new ones, they will not be stored in the database without any error messages or exceptions at all.

Ideally, I would like to be able to perform all CRUD operations with ORM, just like I could do it with a typical DataTable ...

The code that performs the binding is as follows:

        List<Customer> ukCustomers = (from c in diagrams.Customer
                              where c.Country == "UK"
                              select c).ToList();

        customersBindingSource.DataSource = ukCustomers;
        customersBindingSource.AllowNew = true;

My last assumption is that the new rows added by the user in the DataGridView are not part of the list, but are “free-standing” client instances? I would think that they would be automatically added to the list. The same goes for deleted rows that I thought would be automatically removed from the list and that the SaveChanges method from ORM would be able to select this?

- , ? - - , , , WinForms, ORM ( Telerik's)?

.

+3
1

, . " " , , . , , / .

. , / / .

:

  private PropertyManagerModel.DemoDBEntityDiagrams context;
    public Form1()
    {
        InitializeComponent();
        context = new DemoDBEntityDiagrams();
        LoadCommunities();
    }

   private void LoadCommunities()
    {         
        var communityList = new ObservableCollection<Community>(context.Communities);
        communityList.CollectionChanged += new NotifyCollectionChangedEventHandler(communityList_CollectionChanged);
        this.dataGridView1.DataSource = new BindingSource() { DataSource=communityList};
    }

    void communityList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            context.Add(e.NewItems);

        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            context.Delete(e.OldItems);

        context.SaveChanges();
    }      

ORMS, . , !

,

+3

Source: https://habr.com/ru/post/1783123/


All Articles