I am new to ASP.NET programming and for sure I didn't understood clearly how DbSet works in ASP. I tried to search for related questions, but I couldn't find something similar.
I have a database with the tables: Clients and Products.
In the Client table I have a DateTime entry for the expiration of the contract between "me" (the user that uses the software and the Client)
In the Products table I have a DateTime entry for when the product was sold. On this entry I have a Data validation written by me where with the code of the Client I go and retrieve a single row with the SingleOrDefault method.
If the user wants to add a Product with a selling date greater than the expiration of the contract it won't let him.
The problem is: If I (or the user) update the entry for the expiration of the contract with the Client, and then go and want to add a Product with a selling date greater than the old expiration date, but smaller than the new expiration date the Client that I get from the database in debug, in the Validation method has the old Expiration Date and all the old values for that matter.
The Client is definitely being updated since after I update it I look at him in the display Client View and it has the new data.
I have the following hierarchy in the project:
A. A controller for adding and updating the Clients where I have the following code:
// connection to the database
private ApplicationDbContext _context;
// initialize the connection to the database
public GestiuneaDeseurilorController()
{
_context = new ApplicationDbContext();
}
// overrride the Dispose destructor
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
the method that creates and updates the Client is:
public ActionResult CreateClient(clientViewModel clientViewModel)
{
DatabaseConnection databaseConnection = new DatabaseConnection();
if (!ModelState.IsValid)
{
clientViewModel = new clientViewModel
{
Agency = _context.Agency.ToList(),
DateFirma = databaseConnection.assignCompanyData(),
Client = clientViewModel.Client
};
return View("AdddClient", clientViewModel);
}
if (clientViewModel.Client.ID == 0)
_context.Client.Add(clientViewModel.Client);
else
{
Client clientInDb = _context.Client.Single(c => c.ID == clientViewModel.Client.ID);
clientInDb.Name = clientViewModel.Client.Name;
clientInDb.Code = clientViewModel.Client.Code;
clientInDb.Code = clientViewModel.Client.Address;
clientInDb.ContractNumber = clientViewModel.Client.ContractNumber;
clientInDb.ExpiryDate = clientViewModel.Client.ExpiryDate;
clientInDb.Agency = clientViewModel.Client.Agency
}
_context.SaveChanges();
return RedirectToAction("Client", "Clients");
}
The databaseConnection.assignCompanyData() method is a call t retrieve some that from a table. I make this call a lot in the application.
B. a Folder "Validations" in the root folder where I store my validation
the code of the validation that is causing the problem is:
class ValidateSellingDate : ValidationAttribute
{
private ApplicationDbContext _data;
public ValidateSellingDate()
{
_data = new ApplicationDbContext();
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
Product product = (Product)validationContext.ObjectInstance;
if (value == null) return new ValidationResult("Field can't be empty!");
if (product.ClientName == null) return new ValidationResult("Field can't be empty!");
Client client;
try
{
long clientId = long.Parse(client.ClientName);
client = _data.Client.SingleOrDefault(c => c.ID == clientId);
}
catch
{
string code = client.Code;
client = _data.Client.SingleOrDefault(c => c.Code == code);
}
if (client.SellingDate > client.ExpiryDate) return new ValidationResult("Selling date can't be greater than expiry date of contract with the client!");
else return ValidationResult.Success;
}
}
Here I get the old values for the Client with that data. What am I doing wrong?
This might be a very dumb mistake, but I am new to programming web applications.