using System.Data.Entity; using System.Linq; using CardNumbers.Objects; namespace CardNumbers.Data { public class Repository : DbContext, IRepository { public DbSet<Client> Clients { get; set; } public DbSet<ClientOrder> ClientOrders { get; set; } public DbSet<Reorder> Reorders { get; set; } public DbSet<Operator> Operators { get; set; } IQueryable<Client> IRepository.Clients { get { return Clients; } } IQueryable<ClientOrder> IRepository.ClientOrders { get { return ClientOrders; } } IQueryable<Operator> IRepository.Operators { get { return Operators; } } IQueryable<Reorder> IRepository.Reorders { get { return Reorders; } } public void Commit() { this.SaveChanges(); } public void AddClient(Client client, bool autoCommit = true) { Clients.Add(client) ; if (autoCommit) Commit(); } public void DeleteClient(Client client, bool autoCommit = true) { Clients.Remove(client); if (autoCommit) Commit(); } public void DeleteClient(int clientId, bool autoCommit = true) { var ReordersQuery = from reord in this.Reorders where reord.ClientId == clientId select reord; if (ReordersQuery.Any()) // throw "Client " + clientId.ToString() + " can not be deleted because it has related rows in the ReOrders table!"; } public void AddOperator(Operator oOperator, bool autoCommit = true) { Operators.Add(oOperator); if (autoCommit) Commit(); } public void DeleteOperator(Operator oOperator, bool autoCommit = true) { Operators.Remove(oOperator); if (autoCommit) Commit(); } public void AddOrder(ClientOrder clientorder, bool autoCommit = true) { ClientOrders.Add(clientorder); if (autoCommit) Commit(); } } }Since I am going to call DeleteClient method, I am thinking I may need less generic variation and manually test dependencies myself. So, I started to write a version that will delete based on id.
>>public void DeleteClient(int clientId, bool autoCommit = true) >> { >> var ReordersQuery = from reord in this.Reorders >> where reord.ClientId == clientId >> select reord; >> >> if (ReordersQuery.Any()) >> //throw "Client " + clientId.ToString() + " can not be deleted because it has related rows in the ReOrders table!"; >> >> }>>