I am currently developing a project for a university which provides students to create an account and with this account they can calculate their GPA, accessing the forum and screen sharing. I am using ASP.NET MVC and ASP.NET Web Api technologies. I developed my Business Layer and made "Manager" classes that helps to make db operations for all POCOs of my project. All of my manager classes uses same DbContext which is coming from BaseManager class. I have some set of rules in my POCO classes that prevents db insertion if the creedientals are wrong.
When I enter these fields wrong, db insertion fails as expected. But after that when I insert the correct values (entering a value to a field that should not be null etc.) db insertion fails again and same validationerrors shows up as before failed attempt despite that the mistakes are corrected.
Here is my User POCO class(I am sharing the 2 fields that I'm talking about):
[Table("UserTable")]
public class User
{
[Required]
[Index(IsUnique = true)]
[MaxLength(30, ErrorMessage = "Username can not exceed 30 characters!")]
public string Username { get; set; }
[Required]
[Index(IsUnique = true)]
[MaxLength(50, ErrorMessage = "Email can not exceed 50 characters!")]
[DataType(DataType.EmailAddress)]
[RegularExpression("^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3} \\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")]
public string Email { get; set; }
}
When I check the DbSet of my corresponding POCO class, I saw all of my attempted objects also kept locally just like my 2 records on the database. Here's the screenshot:
Here's my Business Layer logic:
My Class View of adjusted version of Business Layer is:
I want to purge dbSet local from insert-attempted objects without restarting the application in order to give chance users to correct their mistakes and create account again.
If I have mispelled or mistaken at some point please forgive me. Any helps would be appreciated. Thanks!
Besides the very valid comment from sellotape about DbContext lifetime, there are a couple of things you can do.
If you want to make the least changes to your app, then I'd suggest to use transactions, eg
using (var transaction = dbContext.Database.BeginTransaction())
{
try
{
// Prepare POCOs to be stored
...
dbContext.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
// Handle failure
}
}
If SaveChanges()
fails, your transaction will be automatically rolled back and all the objects added in this block will be removed from dbContext
.
You can find more info in MSDN on transactions in EF Core or EF 6 .
However, I would suggest you to rethink your validation scheme instead. Data validation is not supposed to happen on the database level - validation should happen much earlier in the request lifetime, maybe even before you create POCOs to be stored to the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.