c# - How to provide a default implementation for LINQ to SQL's OnValidate method? -


I am trying to implement a verification framework for the institutions in the base category. The problem I am facing is getting the OnValidate event to fire properly

The reason is that OnValidate is partially marked so that I can not provide a default implementation in the base class ; This is hidden by LINQ from a new method declared by the SQL class.

How can I fix this so that the OnValidate event in the base class will be automatically asked?

The example code comes down from below.

  Public class EntityBase {public bool ISValid {get {return (GetValidationErrors (.) Calculation () == 0); }} Public Virtual IEnumerable & lt; Validation error & gt; GetValidationErrors () {yield break; } Public Zero OnValidate (System.Data.Linq.ChangeAction operation) {// Call it explicitly in derivative class unless it is never removed. If (IsValid) {stringbinder SB = New stringbilder (); Sb.AppendLine ("Avoid saving verification errors"); Agrech (validation error in GetValidationErrors ()) {sb.AppendLine (String.Format ("{0}: {1}", error. Property name, error. Error message)); } Throwing new application expection (sb.ToString ()); }}} Public partial class LinqThingy: EntityBase {public override IEnumerable & lt; Validation error & gt; GetValidationErrors () {if (String.IsNullOrEmpty (name)) returns new validation error ("name required", "name"); } // Eww bad, do not want to do it. Partial Zero OnValidate (System.Data.Linq.ChangeAction Action) {base.OnValidate (action); }}  

You can override the SubmitChanges (ConflictMode) method in your DataContext class. This will ensure that all organizations get cured just before continuing. In this way you will not have to create a custom base class for institutions. Here's an example of expanding the Data Context Class:

  Public partial class Northwind AccountText {Public override zero} SubmitChanges (ConflictMode failureMode) {EntityValidator.Validate (this.GetChangedEntities ()); Base.SubmitChanges (failureMode); } Private IEnumerable & lt; Object & gt; GetChangedEntities () {ChangeSet changes = this.GetChangeSet (); Change in turn.Institute.ConCat (change updates); }}  

In the above example, the custom verification logic goes into the valid method in the EntityValidator class (you will have to make it definately).

Indicates how to validate organizations with an O / RM technique such as LINQ from SQL, although this enterprise library uses the validation application block, this article may apply in your situation.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -