I have an "Enterprise Resource Planning" project. It uses a SQLite database.
With Entity Framewok, I got my entity classes.
For CRUD operations, I modified my ObservableCollection:
public class ArtistCollection : ObservableCollection<Artist>
{
private ChinookdbContext _context;
public ArtistCollection(IEnumerable<Artist> artists, ChinookdbContext context) : base(artists)
{
_context = context;
}
protected override void InsertItem(int index, Artist item)
{
Context.Artists.Add(item);
base.InsertItem(index, item);
Console.WriteLine("InsertItem");
}
protected override void RemoveItem(int index)
{
Context.Artists.Remove(this[index]);
base.RemoveItem(index);
}
public void Save()
{
try
{
Console.WriteLine("Clienti: Saved N° " + _context.SaveChanges());
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
}
public ChinookdbContext Context
{
get
{
return _context;
}
}
}
This is my modified entity:
public partial class Artist : INotifyPropertyChanged, IEditableObject
{
private Artist backup;
private bool inTransaction;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void BeginEdit()
{
backup = new Artist();
backup.ArtistId = ArtistId;
backup.Name = Name;
inTransaction = true;
Console.WriteLine("- BeginEdit");
}
public void CancelEdit()
{
if (inTransaction)
{
ArtistId = backup.ArtistId;
OnPropertyChanged(nameof(ArtistId));
Name = backup.Name;
OnPropertyChanged(nameof(Name));
backup = null;
inTransaction = false;
Console.WriteLine("- CancelEdit");
}
}
public void EndEdit()
{
if (inTransaction)
{
OnPropertyChanged(nameof(ArtistId));
OnPropertyChanged(nameof(Name));
backup = null;
inTransaction = false;
Console.WriteLine("- EndEdit");
}
}
}
All this for one table.
I would like to be able to proceed in a less verbose way, having to use several tables.
Is there a simpler way?