I am using breezejs in a ASP.NET MVC 4 SPA and durandal project w/ EF 5.0 Database first setup and things are working fine except for a certain scenario.
Forgive me if I seem somewhat of a noob in regards to javascript, and the SPA stuff. I am learning as I go.
here are my 2 models from EF
Project.cs
public partial class Project
{
public Project()
{
this.Timesheets = new HashSet<Timesheet>();
}
public int ProjectId { get; set; }
public Nullable<int> ClientId { get; set; }
public string ProjectName { get; set; }
public string ProjectDescription { get; set; }
public Nullable<decimal> ProjectRate { get; set; }
public virtual Client Client { get; set; }
public virtual ICollection<Timesheet> Timesheets { get; set; }
}
Client.cs
public partial class Client
{
public Client()
{
this.Projects = new HashSet<Project>();
this.Timesheets = new HashSet<Timesheet>();
}
public int ClientId { get; set; }
public string ClientNo { get; set; }
public string ClientName { get; set; }
public string CompanyName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string County { get; set; }
public Nullable<decimal> ClientRate { get; set; }
public string EmailAddress { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<Timesheet> Timesheets { get; set; }
}
an excerpt from my Client viewmodel
function deleteClient(model) {
app.showMessage('Are you sure you want to DELETE this record?', 'Delete a Record', ['Yes', 'No'])
.then(function (dialogresult) {
if (dialogresult == "Yes") {
models.remove(model);
model.entityAspect.setDeleted();
return saveRecord(model);
}
});
};
function saveRecord(model) {
return repository.saveEntity(model)
.fail(handleFailed);
function handleFailed(error) {
var err = "Error retrieving Clients : " + error.message;
error(err);
logger.error(err, null, null, true);
return;
}
};
excerpt from repository.js (datacontext)
function saveEntity(masterEntity) {
return manager.saveChanges().fail(saveFailed);
function saveFailed(error) {
logger.error("Error saving : " + error.message, null, null, true);
}
}
here is excerpt from my ProjectBillingController.cs
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
Here is my scenario,
I have a row Project "P1" which has a reference to Client "ABC". If I try to delete Client "ABC" breeze throws an exception on inside the SaveChanges in my WEBAPI Controller "ProjectBillingController.cs".
I know that in that in SQL world, this should be the expected error.
But my questions are...
- Can I capture this constraint error on the client side before it gets to the server "ProjectBillingController.cs"?
- The only way I see resolving this issue is to do another query to see if there are any rows in Project entity with that Client and if so halt the delete otherwise continue with the delete. Is there another way to accomplish this?
- Should I write this "Validation logic" inside my viewmodel, or datacontent (repository)