दुर्भाग्य से, इकाई फ्रेमवर्क 6 में SQL सर्वर परिवर्तन ट्रैकिंग के लिए अंतर्निहित समर्थन नहीं है। हालांकि, यह अवरोध क्षमताओं का पर्दाफाश करता है जो आपको निष्पादन से पहले जेनरेट किए गए एसक्यूएल को संशोधित करने में सक्षम बनाता है। एसक्यूएल को बदलते समय एक ओआरएम उत्पन्न करता है वह कुछ ऐसा होता है जिसे ध्यान से किया जाना चाहिए और केवल अच्छे कारण से, बिल्कुल ऐसे मामले हैं जहां यह उचित समाधान है।
ईएफ 6 IDbCommandInterceptor
प्रकार का खुलासा करता है, जो आपको पूरी क्वेरी पाइपलाइन में हुक देता है। आपको इस इंटरफेस को लागू करने और ईएफ के साथ अपने इंटरसेप्टर को पंजीकृत करने की सरल आवश्यकता है।
विशेष रूप से, ढांचा हर INSERT
, UPDATE
से पहले NonQueryExecuting
, और DELETE
फोन, आप अपने परिवर्तन ट्रैकिंग में हुक करने के लिए यह एक महान जगह बनाने होंगे।
public class ChangeTrackingInterceptor : IDbCommandInterceptor
{
private byte[] GetChangeTrackingContext()
{
// TODO: Return the appropriate change tracking context data
return new byte[] { 0, 1, 2, 3 };
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
command.CommandText = "WITH CHANGE_TRACKING_CONTEXT (@change_tracking_context)\r\n" + command.CommandText;
// Create the varbinary(128) parameter
var parameter = command.CreateParameter();
parameter.DbType = DbType.Binary;
parameter.Size = 128;
parameter.ParameterName = "@change_tracking_context";
parameter.Value = GetChangeTrackingContext();
command.Parameters.Add(parameter);
}
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
}
जब एफई किसी भी प्रश्न है कि डीबी के राज्य में परिवर्तन उत्पन्न करता है, यह क्वेरी निष्पादित करने से पहले इस विधि कॉल करेगा:
एक साधारण उदाहरण के रूप में, यह इंटरसेप्टर पर विचार करें। यह आपको मानक एसक्यूएल का उपयोग कर अपने कस्टम परिवर्तन ट्रैकिंग संदर्भ को इंजेक्ट करने का अवसर प्रदान करता है।
एफई के साथ अपने इंटरसेप्टर रजिस्टर करने के लिए, बस अपने स्टार्टअप कोड में कहीं DbInterception.Add
फोन:
var changeTrackingInterceptor = new ChangeTrackingInterceptor();
DbInterception.Add(changeTrackingInterceptor);
वहाँ IDbCommandInterceptor
इंटरफेस पर महान दस्तावेज की एक टन नहीं है, लेकिन this MSDN article एक अच्छी जगह शुरू करने के लिए है।
यह सुनिश्चित नहीं है कि मुझे आपका प्रश्न सही है - मैंने जो अनुमान लगाया है, उससे आप ईएफ के माध्यम से अपडेट किए जाने पर chnage_tracking निष्पादित करना चाहते हैं। हो सकता है कि आप अपने अपडेट/एड/डिलीट के लिए procs (आपको जिस ट्रैकिंग की आवश्यकता हो) संग्रहीत कर सकते हैं और फिर इसे ईएफ इकाइयों में मैप कर सकते हैं। यहां अधिक जानकारी - http://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-procedure-mapping.aspx। अगर मैं आपका प्रश्न गलत पढ़ता हूं तो मुझे बताएं .. – Developer