One: The injection or interception or expansion of EF
1. DBContext ... Is there a point to be injected ...
There are a lot of override behaviors under controller ...
"1" shouldvalidateentity whether the entity needs to be verified
Trigger point: SaveChanges (curd) some operations.
For these operations, we may have some entity-specific validation actions ...
Jquery.validator ...
The operation of the binding in MVC. "Dataanotation"
"2" validateentity Specific verification behavior
The analysis of shouldvalidateentity angle in the source code of "3" SaveChange
Use Ilspy to fix ...
//System.Data.Entity.Internal.InternalContext
Public virtual int SaveChanges ()
{
int result;
Try
{
if (this.) validateonsaveenabled)
{
Ienumerable<dbentityvalidationresult> validationerrors = this. Owner.getvalidationerrors ();
if (validationerrors.any<dbentityvalidationresult> ())
{
throw new dbentityvalidationexception ( Strings.dbentityvalidationexception_validationfailed, validationerrors);
}
}
BOOL flag = this. Autodetectchangesenabled &&!this. validateonsaveenabled;
saveoptions options = Saveoptions.acceptallchangesaftersave | (flag?) SaveOptions.DetectChangesBeforeSave:SaveOptions.None);
result = this. Objectcontext.savechanges (options);
}
catch (Updateexception updateexception)
{
Throw this. Wrapupdateexception (updateexception);
}
return result;
}
Public ienumerable<dbentityvalidationresult> getvalidationerrors ()
{
list<dbentityvalidationresult> list = new list<dbentityvalidationresult> ();
foreach (Dbentityentry current in this. Changetracker.entries ())
{
if (current. Internalentry.entitytype! = typeof (Edmmetadata) && this. Shouldvalidateentity (current))
{
Dbentityvalidationresult Dbentityvalidationresult = this. Validateentity (current, new Dictionary<object, object> ());
if (Dbentityvalidationresult! = null &&!dbentityvalidationresult.isvalid)
{
List. ADD (Dbentityvalidationresult);
}
}
}
return list;
}
Three: Verification of Studentname
protected override Dbentityvalidationresult validateentity (Dbentityentry entityentry, Idictionary<object, Object > Items)
{
Get started with custom validation: for example, the value of our studentname must not be "test". If it is test, it will throw an exception
Return base. Validateentity (entityentry, items);
}
if (this. validateonsaveenabled)
{
Ienumerable<dbentityvalidationresult> validationerrors = this. Owner.getvalidationerrors ();
if (validationerrors.any<dbentityvalidationresult> ())
{
throw new Dbentityvalidationexception (strings.dbentityvalidationexception_validationfailed, validationErrors);
}
}
Four: In the Curd database level interception, monitoring, so that the convenience of our record log ...
Core interface: Idbcommandinterceptor
1. Custom class Implementation Interface
2. Inject into the Entity Framework ... Through the form of appconfig ...
UPDATE [dbo]. [Student]
SET [Studentname] = @0
WHERE ([StudentID] = @1)
SELECT [RowVersion]
from [dbo]. [Student]
WHERE @ @ROWCOUNT > 0 and [StudentID] = @1
Logging ...
DataBase.Log method ....
Custom validateentity entity validation and DB level "interception" intercept execute in EF