Closed MikeAtTheMill closed 1 year ago
@MikeAtTheMill you're going to have to provide some code to show what you're doing - it's not possible for us to help without more info beyond "SaveChanges did not persist". Please submit a minimal, runnable code sample that shows what you're trying to do.
https://learn.microsoft.com/en-us/ef/ef6/fundamentals/databinding/winforms Sorry. I just copy / paste the code in the example
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.Entity;
namespace WinFormswithEFSample
{
public partial class Form1 : Form
{
ProductContext _context;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_context = new ProductContext();
// Call the Load method to get the data for the given DbSet
// from the database.
// The data is materialized as entities. The entities are managed by
// the DbContext instance.
_context.Categories.Load();
// Bind the categoryBindingSource.DataSource to
// all the Unchanged, Modified and Added Category objects that
// are currently tracked by the DbContext.
// Note that we need to call ToBindingList() on the
// ObservableCollection<TEntity> returned by
// the DbSet.Local property to get the BindingList<T>
// in order to facilitate two-way binding in WinForms.
this.categoryBindingSource.DataSource =
_context.Categories.Local.ToBindingList();
}
private void categoryBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
// Currently, the Entity Framework doesn’t mark the entities
// that are removed from a navigation property (in our example the Products)
// as deleted in the context.
// The following code uses LINQ to Objects against the Local collection
// to find all products and marks any that do not have
// a Category reference as deleted.
// The ToList call is required because otherwise
// the collection will be modified
// by the Remove call while it is being enumerated.
// In most other situations you can do LINQ to Objects directly
// against the Local property without using ToList first.
foreach (var product in _context.Products.Local.ToList())
{
if (product.Category == null)
{
_context.Products.Remove(product);
}
}
// Save the changes to the database.
this._context.SaveChanges();
// Refresh the controls to show the values
// that were generated by the database.
this.categoryDataGridView.Refresh();
this.productsDataGridView.Refresh();
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
this._context.Dispose();
}
}
}
I am running the example. Then I switch to SSMS and check the tables in the database. I am NOT using the localdb - I am using a developer edition of SQL Server. I check both tables by using SELECT * FROM etc. They are empty my friend.
Hold on. The problem was in the app.config. It works. Sorry to bother you.
This issue tracker is for documentation
For product issues, use https://github.com/aspnet/EntityFramework/issues I followed the database first approach. I'm using VS 2019 community edition. And SQL Server & Tools 18. Don't know why it doesn't work. But SaveChanges did not persist the updates to the database. I checked the tables using SSMS w/ T-SQL selects. Nothing in either table. Sorry folks.