Custom validateentity entity validation and DB level "interception" intercept execute in EF

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.