रिकॉर्ड पढ़ने की कोशिश करते समय मैं ईएफ के साथ संघर्ष कर रहा हूं, फिर उसी रिकॉर्ड में उन रिकॉर्ड्स को हटा दें। मैं शुरू में EntityState.Deleted विधि, जो त्रुटि देना होगा उपयोग कर रहा था:EntityState.Deleleted काम नहीं करता है, निकालें (इकाई) करता है?
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
लेकिन अगर मैं मैं नीचे है पसंद करने के लिए इसे बदलने, .remove() का उपयोग कर, तो सब कुछ ठीक है।
- उपयोग करने के लिए क्या अंतर और सर्वोत्तम समय है। Remove() बनाम हटाया गया?
- मैं इस काम को। हटाए गए विधि का उपयोग करके कैसे कर सकता हूं? मैंने पढ़ने के लिए मेरे भंडार में संदर्भ का एक नया उदाहरण बनाने और दूसरे को हटाने के लिए प्रयास किया है, लेकिन फिर आईईएनटीटीट्रैकर से संबंधित त्रुटियां कई उदाहरणों को ट्रैक नहीं कर सकतीं ... मैंने भी कोशिश की। निर्भर रिकॉर्ड लोड करने के लिए प्रारंभिक पढ़ने को शामिल करें ईएफ में तो यह जानता है और उन्हें हटा देता है। मैंने भी कोशिश की। पहले पढ़ने के रिकॉर्ड डालने। सभी का कोई फायदा नहीं हुआ।
प्रश्न में विधि यहां दी गई है। ध्यान दें कि मैं एक सामान्य भंडार जो .Deleted विधि है जो मुझे इस परिदृश्य जब तक अच्छी तरह से सेवा की है का उपयोग करता है (तब पढ़ने ही रिकॉर्ड को हटाने।)
//Delete Allocation Need and AllocatedContainers for alloc need id
public ActionConfirmation<int> DeleteAllocRecords(int intFacilityId, AllocNeedSourceTypes needSourceType, int intNeedSourceId)
{
var context = new InventoryMgmtContext();
var repository = new AllocationNeedRepository(context);
//Delete Allocation Need and hence children in Allocated Containers
var srcType = needSourceType.ToString();
List<AllocationNeed> allocNeeds = repository.SearchFor(
x => x.FacilityId == intFacilityId
&& x.NeedSourceType == srcType
&& x.NeedSourceId == intNeedSourceId
).ToList();
//var deleteRepository = new Repository<AllocationNeed>(); <--tried separate instance of context to delete...no worky.
foreach (AllocationNeed allocNeed in allocNeeds)
{
try
{
//NO WORK: context.Entry(allocNeed).State = System.Data.EntityState.Deleted;
context.AllocationNeeds.Attach(allocNeed);
context.AllocationNeeds.Remove(allocNeed); <-- Works
context.SaveChanges();
}
catch (Exception ex)
{
return ActionConfirmation<int>.CreateFailureConfirmation(ex.Message, allocNeed.Id);
}
}
उपयोग करने का कोई वास्तविक कारण नहीं है। मुझे पता है कि मैंने उस विधि का उपयोग करने के लिए पहले से ही अपने जेनेरिक भंडार को लिखा है, लेकिन यह कोड का एक पंक्ति है। हटाने विधि, इसलिए मैं इसका उपयोग करूंगा । धन्यवाद! –
निकालें बच्चे की वस्तुओं को नहीं हटाएगा। यदि बच्चे की वस्तु का एफके निरर्थक है तो यह इसे शून्य पर सेट कर देगा, यदि आवश्यक हो तो यह अपवाद फेंक देगा। बाल वस्तुओं को हटाने का तरीका कोड-फर्स्ट या डीबी-फर्स्ट में रिश्ते को 'कैस्केडऑनडेले' के रूप में चिह्नित करना है। – Shoe
^-> या नीचे-अप हटाएं। – JoeBrockhaus