I have a Linq-to-SQL class, and I'd like to perform some pre-save validation before the record is persisted to the DB. In addition, once it has been saved, I'd like to have some post-save processing code.
Similarly, when a record is deleted, I'd like to have pre- and post- methods that will be called, no matter from where the context.SubmitChanges()
call is made.
I don't see any methods in the generated code that I can override. The partial method OnValidate()
may be sufficient for the pre-processing, but I want to ability to cancel the save if certain conditions are not met, and I don't see any hooks at all for post-processing.
Am I missing something? Or can you recommend another way of achieving the intended effect?
Thanks!
The LINQ to Objects provider enables you to query in-memory collections and arrays. If an object supports either the IEnumerable or IEnumerable<T> interface, the LINQ to Objects provider enables you to query it.
In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source ( customers ) and the range variable ( cust ).
The foreach statement is also where the query results are retrieved. For example, in the previous query, the iteration variable num holds each value (one at a time) in the returned sequence.
LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution. You can also force a query to execute immediately, which is useful for caching query results.
For inserts:
Create a base class for your entities.
Add some virtual methods.
Override DataContext.SubmitChanges
.
Call DataContext.GetChangeSet()
and iterate over the inserts applying the function you defined previously.
For reverting changes, look at my extension method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With